When human infants are born, they have the ability to sense when something is wrong like being hungry, having gas, being cold, and so on–even before they could tell you exactly what it is–and to cry out about this. The result of their crying out is that someone comes and takes care of whatever is wrong for them. So, experience teaches them–let’s go right to the meat of it–that prayers are answered.
What can go wrong? If they don’t cry when hungry or cold, that is, if they don’t pray for relief, they tend to become sickly and die. Alternatively, if they do cry, but their prayers are unanswered, that it, no one arrives to take care of the problem for them, then they also tend to die. So, humans that survive this awkward early period pretty much tend to pray as a matter of course, and know that prayers are answered. There will be exceptions of course, but the general rule will be clear. (For instance, an exception: If you were so well taken care of by persons very attentive to all your needs so that you never had to cry, or did less of it, you would not learn that prayers were answered.)
So, later in life but quite early in life still, this general situation stops being true. They are encouraged not to cry and to be specific about what it is they want. When they reach a maturity level where they can actually do this, what they find out is that the situation of distress is often not fixed automatically. They find out that food is not immediately forthcoming, that they may be required to fix their own problems, and that crying is viewed as immature and bad. This is the awful period of transition to being a human being.
But here’s the thing: The belief in prayer is fixed at a very early age, and although all memories of that hyper-immature formative state of life large disappear for the young human, and although experience will make it very clear that prayers have very little effect and are not answered at all, the basic neurological wiring is there supporting the “Ask and it shall be given” even when all the events of that period are forgotten and later events contradict it.
This explains the existence of belief in prayer and the continued belief in it even when experience does not actually support it. This is why it is said that there are no atheists in foxholes–or at least very few of them.