Morality ultimately comes from the needs of society. In the 1800s, in certain parts of the world it was considered morally upright to own slaves. The Bible contains guidance from various eras on how to treat your slaves.
All of civilization is based on structured violence, it would be impossible to have something big and grand like a city without people around to do the things nobody wanted to do, and the only way to get people to do things they don’t want to do is to compel them with some kind of force.
When you strip the violent trappings of this compulsion, you call what’s left morality. You’re still forced to do things you don’t want to do, but nobody other than yourself compels you into doing them. We all have jobs that most of us hate, but we get up and do them anyway because otherwise we wouldn’t be able to participate in society.
It’s way better to have moral compulsion than to have violent compulsion. You really don’t want Starbucks sending armed soldiers to your door in the mornings to force you to come to work.
But it’s still structured violence and it still sucks to be subjected to it. The natural human response to violence is to try to make some kind of meaning out it, why does it have to be this way?
The various religions of the world are attempts at answering this question. They are, fundamentally, forces of democratization. Rather than ugly rule by force, religion seeks a better way to order society. Most people will only listen to imposed moral codes when it suits them, but that’s exactly the problem religion seeks to deal with.
The use of force to compel work, ultimately doesn’t scale. There’s just not enough soldiers out there to whip everyone into compliance, all the time. People will simply revolt and brave mortal danger against your soldiers, and if you kill them, you don’t have anyone left to do the work you want them to do.
So rulers have to maintain a peace truce with the ruled. Religion has traditionally been the way this was accomplished. A society’s god anoints the ruler and gives him a divine mandate to rule. He has to maintain religious traditions set out by the clergy. He has to be seen as the sort of person that their god would anoint as a ruler.
Religion is the way to get an equitable distribution of the structured violence needed to operate an agrarian civilization. The king may want to just laze around all day and get fat, but if he strays too far off of message, it leads to revolt.
As civilizations grew and the violence needed to maintain them lessened ever so gradually, ideas of what’s good and moral changed, as did the religions they were based on. The ancient Hebrews invented the idea of ethical monotheism, the conception of all morality as coming from God. This idea stems from the observation that humans are innately selfish, given free reign, they’d act as beasts rather than as rational thinkers. So where did this idea of goodness come from if not from mankind?
It couldn’t have come from man, but it’s still a rational concept. Can you see the conundrum? Who could think up this idea of goodness and give it to people so that they could learn how to be good? The answer from their point of view could only be God. And so the Hebrews managed to abstract their deity beyond the norm for the time. Every society thinks their god is the best god, but only the Hebrews could articulate a vision of a truly universal God. But this is a history of the concept of morality, not of religion, so I won’t go too deep into how that evolved.
Reason, wisdom, justice, humility all could become virtues once society stopped needing to compel people to do the backbreaking work of building civilization. And these concepts all evolved out of the shared social compact that allowed people to derive meaning out of their lives that were, and still are, ordered by structural violence.
Any attempt to define something good, something moral, has to be accommodating to all of society. So society is the ultimate source for morality. The ancient Greeks invented a particular way of working with knowledge, that we now call philosophy. Out of this philosophy come our ideas of rationality. Rationality ordered the further evolution of religion and moderated its weak points. This modified religion built the modern world of plenty we now exist in, and we as a society have been flirting with discarding religion and ordering our morality on pure reason for the last few hundred years.
It’s been variously successful. Separating church and state cannot be done violently, as is practiced in totalitarian regimes. Society becomes brittle and everybody just loses the motivation to do anything, the state has to compel everything. But in the modern developed nation-state, a slow transition from now-provincial frames of thought seems to only be beneficial for society as a whole.