For some who take an evolutionary perspective on humanity while at the same time taking a rather dim view of religion, it can seem strange that religion would persist with the strength with which it has persisted in human social groups. After all, the fact that our brains appear to be "wired" to believe in strange agencies external to ourselves doesn't get us to religion, though it might get us to various conspiracy theories.
So our biology would certainly appear to explain our tendency toward conspiracy theories about lizard people, evil spirits, or the Illuminati and so on. But what explains, from an evolutionary standpoint, the social institution of religion, whether at a tribal level or the level of an international empire? We could appeal to the notion that religion provides shared values which can unite otherwise disparate social groups and thereby shore up the sorts of bonds that allow these groups to maintain the shared identity necessary for seeing themselves as part of the same group and functioning as such to maintain the integrity of the group against outside forces competing for their resources. This might explain emperor worship in various places throughout history.
And that's really not a bad explanation, as far as it goes. But what explains the complex eschatological theology and assigning all sorts of superlative attributes to deities? Those don't seem necessary to social coherence. We might suggest that it is a comfort to people who are suffering and grieving to think that a superlatively important being cares about their problems and is looking out for them in some fashion, perhaps sending them to a pleasant afterlife. This too, has its limits, quite aside from the fact that far from all religions have deities with superlative attributes.
If it were merely a matter of psychological comfort that allows us to continue to function under pressure, then why would we have horrifying hell dimensions as a standard part of religious eschatology? It might make sense as a psychological comfort if it were exclusively used as a tool to tell ourselves that those evil people will get what was coming to them, but we often find that people are afraid of going to those hell dimensions themselves. Doesn't that put them under more psychological pressure and potentially impair their chances at survival? Wouldn't we expect that such a damaging thing would have been naturally selected out of human religious belief long ago?
It seems to me that if we are going to account for the existence of religion as an evolutionary phenomenon in all its variety, then we need to find another piece of the puzzle. I would like to propose a possible candidate for that piece, keeping in mind that I am not an expert in the fields of evolutionary psychology, anthropology, et cetera.
Altruistic behavior is key to the survival of human social groups, particularly altruistic behavior in which an individual makes a sacrifice which is costly to themselves for the benefit of the group. We might think that this in-group altruism would be fostered by religion and thereby support the survival of the group. There are a couple of problems with applying this idea in a simplistic, straightforward manner.
First, not all religions are altruistic in their moral outlook, and some are far from it. But these religions (or aspects of a religion that is only somewhat altruistic) might be explained by reference to social coherence and psychological comfort, so this is not necessarily a large problem. Second, not all religious moral outlooks are limited to altruism with regard to the in-group. Many of us are probably familiar with the prescription of Jesus of Nazareth that we love our enemies, and this extends an altruistic moral outlook beyond the in-group, making it difficult to completely limit the effect of religion to the obvious in-group survival imperative (though we might argue that it merely broadens it to the survival of the species).
The ability of religion to foster altruism might be helpful for survival in some cases, but it does not seem that it is an inherent function of religion across the board. So what could be an inherent function of religion which is plausible as an aid to survival and not idiosyncratic to a subset of religions? I would suggest that under harsh survival pressure, the most valuable way in which religion would contribute to survival in addition to previously discussed factors is by providing an extraordinary motivation.
This extraordinary motivation could be a number of things within the religion. Perhaps some would be driven by a fear of a terrible, torturous afterlife to act in the perceived interest of the group. Perhaps others with a somewhat more positive moral outlook would be driven to act in the perceived interest in the group because they wanted a pleasant afterlife. Perhaps the more mature individuals would find themselves in possession of a genuine regard for their fellow group members because of their acceptance of religious values and that would provide their extraordinary motivation for acting in the perceived interest of the group. Perhaps their extraordinary motivation would be grounded in something more like a mystical relationship with the divine or philosophical truth.
Regardless of the quality of their moral development, people can find an extraordinary motivation in a religion, something that can drive them to act when it is difficult to act or to lay down their lives when it is difficult to lay down and die. The survival value of an extraordinary motivation is fairly clear from the human experience and from history; motivation has helped to turn the course of personal and communal history many times. When under high survival pressure, it is incredibly useful for a human being to have an extraordinary motivation to keep them from crumbling into debilitating depression or fatalistic battle weariness. An extraordinary motivation can keep us fighting for our survival long after our reason has told us that the cause is lost, that we should give up and die. An extraordinary motivation can keep someone like Mahatma Gandhi or Martin Luther King Jr. going against overwhelming odds and constant stress.
I am not inclined to argue that extraordinary motivations can only come out of religious conviction, but I would suggest that successful religions are structured in such a way that it is easy for most people to find an extraordinary motivation in them, and that having a set of extraordinary motivations readily available to people would be and has been quite valuable to human survival. People do often need some great sense of motivation upon which they can hang firmly to carry on under difficult circumstances.
There are of course times when this extraordinary motivation can be a problem for human survival, and not just because it might be a motivation that leads then to commit terrible acts of violence against other human social groups. Sometimes an individual or subset of a group will have pathologies which lead them to turn against members of the larger group and commit terrible acts of violence against people they might be expected to see as part of their in-group.
As with many other strategies for survival, there are times when it can become counter-productive. We see the same phenomenon with other traits such as aggressiveness; it is a trait that is incredibly useful in saving the group in some circumstances and can get them all killed under other circumstances. This does not mean that it is not valuable to our survival on the whole, but it does mean that it is a survival strategy that comes with attendant risks, as they all do.