Perhaps it can be better put another way.
Imagine you are a king or ruler of a dark ages civilisation. Now imagine that you are of the personality type that you have a strong sense of empathy for others, which in turn has given you a strong sense of right and wrong.
Your task is to somehow bend and coerce your subjects to your way of thinking. In your head you want them all to laugh, play and be happy with each. In reality they are a mixture of people who either want to stab, rob, sleep with and sometimes get along with those around them. How do you alter their ways?
1) You punish them, very harshly and often in the hope they will learn like an animal does.
2) They are cleverer than animals though, you might get martyrs and the like, plus you don't like doing all that stuff, so how about using an official document, written by no one man and in fact said to have come from a supernatural source. Many of them cannot read, they don't understand science, the world around them scares them in many ways, so when they are told all about heaven and hell, God and the Devil, by a learned individual who CAN read and is respected, what are many of them going to do? A God fearing fledgling society is a society that can be mostly controlled without the need for capital punishment.
Now of course, the majority of people in the parts of the world where religions are waning ARE equivalent to those learned individuals. Due to a good education system, most aren't in awe of things like magnetism any more. The same can be said for a knowledge of right and wrong and learning to understand the feelings and thoughts of others.
Of course, there is still a large part of society in which religion is still passed down, and combating the effects of parenting is a very difficult and some would say bad thing to do. There has also been thousands of years for people to muse over these teachings and they simply transcend even their original meaning.
As I said earlier, religion is not something to be hated and stamped out, it will slowly filter it's way out of society in any sense beyond being a curiosity, or simply a positive driving force in peoples lives (and nobody can argue that currently it can be a VERY negative driving force in that it offers reinforcement of bad beliefs to some). Religion IS humanity, its a human construct and it's a very key indicator as to how we collectively think as they are written down and rationalised.
Personally I prefer to define religious texts as being well worded philosophical texts rather than instruction manuals, given all that is written in the Bible or any other holy book was written by humans (ignoring the fact that some state their reference was supernatural), where is the major difference between the teachings of Plato and the teachings of the Bible aside from the fact the author is truly known for one and the other is effectively a collection of short stories by different contributing authors!