This comes from the supposed habit of ostriches hiding when faced with attack by predators. The story was first recorded by the Roman writer Pliny the Elder, who suggested that ostriches hide their heads in bushes. Ostriches don't hide, either in bushes or sand, although they do sometimes lie on the ground to make themselves inconspicuous. The 'burying their head in the sand' myth is likely to have originated from people observing them lowering their heads when feeding.
The story also relies on the supposed stupidity of ostriches, and of birds in general. In fact, there's little to support that either as birds have a significantly larger brain to weight ratio than many other species of animal. The notion is that the supposedly dumb ostrich believes that if it can't see its attacker then the attacker can't see it. This was nicely reformed as a joke on Douglas Adams' 'Hitchhiker's Guide to the Galaxy', in which the 'Ravenous Bugblatter Beast of Traal' was described as 'so mind-bogglingly stupid that it assumes that if you can't see it, then it can't see you.'