Natural selection favoured a rather small and inconspicuous group of mammals who would survive by working co-operatively together in small groups. These groups needed to communicate in order to work together. The better they were able to communicate the better was their chance of survival.
As that ability to communicate developed so did the mammals need to express the survival imperative (for their genes) of being part of a community : to codify and embed that survival strategy in the idea that each human animal was necessarily part of something that transcended its own individual survival. That transcendent other was expressed in religious terms from the earliest human societies onwards. But it is a mythology (NB Mythology is an extended metaphor) or as Richard Dawkins would rightly observe an illusion (the God Illusion, or The Selfish Gene) but an illusion that was important for the survival of the human species at the beginning of its development.