Christopher Boehm believes that social communities getting together to punish deviance had a lot to do with the development of conscience and the human moral sense - a sense seemingly lacking even in our closest animal relatives. Hunter-gatherers (HG) are good at that, and modern humans have carried that over into small towns, churches, and other groups.
Mostly our HG ancestors have been concerned with behaviors that directly threaten the group survival: bullying, psychopathy, excessive murder, can rate the death penalty, but lesser crimes are first dealt with by shaming and threats of exclusion.
More modern societies extend the list of condemnable behaviors considerably, often in the form of various religious prohibitions and shibboleths. I'm thinking here of everything from tatoos to dietary restrictions to rules about who can marry whom. What sociobiological function, if any, do such prohibitions and rules perform?
My guess is that the central function is to weld the members into a unified community. By drawing sharp, if largely imaginary, boundaries between groups, people are forced, or at least incentivized to draw a boundary on the "us" side and against the "them" of the outsiders. Think of it as the cultural equivalent of a cell membrane, or a vertebrate skin.