It is my understanding that this behavior is the byproduct of evolution. Please forgive my very basic, rudimentary, and incorrect-in-some-places explanation of how I understand the evolution and humanity's current obsession with believing in things that don't exist.
It starts with certain chemicals encountering each other in one way leading to a reaction, and encountering in another way lead to the reaction not happening.
Move forward through time a bit and you've got masses of chemicals naturally gravitating toward certain interactions with other masses of chemicals which lead to certain reactions, and gravitating away other masses of chemicals which don't result in certain reactions.
You have small lifeforms going after food and avoiding predators. Senses develop in these lifeforms which can detect environments wherein it can find food and avoid predators.
Then you have information processing systems developing which are able to do pattern matching in order to identify where good things might be and bad things might not be. Increasing complexity in this system gives rise to the lifeform beginning to be able to predict places, behaviors, etc. which can lead to positive and negative outcomes for itself.
Increasing in information processing complexity, the lifeform begins to learn causes of the situations that can lead to positive and negative outcomes. In order to maximize its own benefit, it actively starts to reason out unknown causes.
Still more complexity leads to entire frameworks of understanding of the world around the lifeform being imagined.
Conditioning to external stimuli, combined with unknown causes, leads to superstition. When causes for events are simply unknowable for the lifeform, it extends the concepts that it knows about - e.g. other lifeforms, etc. - and invents an "unknown actor" (i.e. god) as the cause, trying to come up with the best framework for predicting what behaviors it needs to perform in order to maximize its benefit and minimize its harm.
This framework evolves in social lifeforms to become a series of rituals and, eventually, religious beliefs.
These religious beliefs are, in effect, environmental conditioning. This condition becomes hard to break, since it's tied, evolutionarily, to the lifeform's successful existence.