Religion developed because primitive humans needed to attempt to understand things which they did not, so they applied agency to these things. Example, the sun rose and provided warmth, safety from predators, helped crops grow, etc. They did not understand why a giant sphere did this, and to make sense of the world, they began beleiving this thing which provided so much for them must be some sort of deity - hence, they now understood the sun in the best, yet most primitive way they could. This all arises from innate survival instincts which evolved in us over time. When a primitive man saw the grass move, he applied his best primitive instincts - possible predator - applied agency.
The same can be said of many of the prehistoric paganistic type religions and beliefs. The moon is another example. Fire is an example. I could go on, but basically, in order to better understand things we could not control, we had to develop "religion."
Many of the monotheistic religions only arose as a means of understanding and coping with death, and "good and evil" as well as a means for control of people and to use fear as a way of making people follow rules. When a drought, a storm, an earthquake, etc happpened, they attributed these natural disasters to punishments from their gods for bad behavior. Ritualistic prayer is also a way for humans to express themselves and cope with trying times, because it takes the responsibility out of their hands if they think a deity is controllilng everything and that his will final.
This is the tip of the iceberg, and I could go on. But for a good read, checkout "Breaking the Spell" by Daniel Dennett