This question is a big part of what the science of morality studies.
Studies in the science of morality to date support the hypothesis that past and present cultural moral norms exist because they solve cooperation problems. Formally, “cultural moral norms are parts of strategies that solve cooperation problems”.
Claiming that the science “supports” this hypothesis means that the hypothesis 1) explains virtually all past and present cultural moral norms, 2) is not contradicted by any known facts, and 3) meets other appropriate criteria for scientific truth such as simplicity and integration with the rest of science.
As we might expect, behaviors that create cooperation problems (the opposite of cultural moral norms function) are consistent with moral norms that define what is immoral. For example, “do not steal, lie, or kill” forbid behaviors that create cooperation problems that prevent or inhibit future cooperation.
What about ethical judgments that may have little to nothing to do with cooperation? For example, vegetarianism can be an individual ethical choice. But an individual’s ethical choice is not a cultural moral norm. Individuals’ ethical choices can have other sources than cooperation strategies. For example, those ethical choices could be based on rational thought about questions such as “What is good?”, “How should I live?”, and “What are my obligations?” which are subjects that extend beyond cooperation strategies.
Cultural moral norms are norms whose violation is commonly believed to deserve punishment, though the person may not actually be punished. How, or if, a person is punished appears to be often calibrated to maximize the likelihood of future cooperation. This is what we should expect if moral norms are parts of cooperation strategies.
Punishment, or the threat of punishment, of violations is necessary for the stability of cooperation strategies. If the free rider who wants to accept help from others but refuses to reciprocate is not punished, then the cooperation that provides that help is unsustainable.
Cultural moral norms exist because they solve cooperation problems. For example, versions of the Golden Rule, “Do not steal, lie, or kill”, “eating pigs is an abomination”, “masturbation is a sin”, “women should be submissive to men”, and “homosexuality is evil” are all parts of known cooperation strategies from game theory.
Understanding why they exist and how they work removes the veil covering their often mysterious and mystical origins and provides an objective basis for resolving disputes about if they will be advocated and enforced in a society.