Although there is an inarguably strong tendency for people to perceive justness and fairness in the world, and particularly in their own life, few would argue that the world is entirely fair. This paradox between the awareness that things are not always fair and the human need to perceive justice and order in the world is quite interesting. It not only is potentially damaging psychologically, but also at a societal level. If people perceive that things are too unjust, then they may be motivated to take the law into the own hands. The existence of any society then hinges in part on how people navigate the unfairness that they perceive around them. Indeed, there is strong evidence to suggest that people go to great lengths to not perceive injustice. But, in addition to ignoring and/or justifying the injustice (such as blaming victims), evolutionary psychologists have suggested that religious beliefs might play a role in helping to maintain the sense that the world is ultimately fair and just.
Evolutionary psychologists have specifically argued that the belief in punishing Gods evolved as a means of preserving groups and societies amidst the inevitable injustice that comes with being human. By believing in punishing God(s), people can come to terms with the injustice around them by perceiving that, basically, God will get them anyway. In turn, if a person does get away with a crime or major injustice, then it becomes less likely a group member will come after them. God belief is particularly good for this, because God is of course believed to be all powerful and all knowing. Hence, believing in Divine punishment for the individual is much more suited to maintaining a sense of justice in the universe than, say, believing in the infallability of government leaders to preserve order (though they are still somewhat interchangeable).
Despite the logic of these ideas, they still needed to be tested. Arguably the most direct tests to date are 5 studies headed by Kristin Laurin, a psychologist at Stanford University, and Azim Shariff, a psychologist at the University of Oregon. In these studies, they made a distinction between how religious people are overall, and their specific belief in powerful, controlling God(s) who can dole out punishment. These beliefs were assessed, and then participants played a game in which they were told that player A had cheated player B when having the sole authority to split $20. The participant (always player C) was then able to keep their $10, or use that $10 to reduce the amount that player A—the cheater—got. Participants used their money less to punish Player A when they had a high belief that God is punishing (overall religion worked the opposite). This was especially likely when their religious beliefs were primed in the study (i.e., they answered religious questions directly prior to the study). Subsequent studies found that belief in a powerful God (or Gods) made people less likely to want to punish moral violators independently of potentially gaining from not punishing them. This is consisent with the notion that participants were not merely afraid that God(s) would punish them for harming others, which is arguably a confounding variable in the other studies.
Together, these studies support the evolutionary psychology argument that belief in punishing God(s) protects people from wishing to dole out punishment themselves when they perceive injustice. As injustice tends to cause anger (and hence, motivates aggression), this indicates that people can use belief in a punishing God to maintain the sense that, ultimately, the world is fair and just.
Bad things will always happen, to all of us, good or bad. People will get cheated and things are not always fair. But these studies suggest that humans may have evolved specific belief in Divine punishment to help cope with the injustices they face. The result is not only less psychological distress for the individual, but arguably a greater likelihood that the group/society itself will survive and remain relatively harmonious.
The notion that God is a sort of cosmic avenger who will restore justice to the universe protects people's need to believe in justice and order, and in turn, makes them less morally punitive towards moral violators.