In 1973 the late science fiction author Robert A. Heinlein gave an address to the Naval Academy (he was a graduate thereof) in which he spoke on this very subject—and in which he showed the evolutionary origin of moral behavior. What’s interesting is how he defined moral behavior. Here is the relevant excerpt from the address:
As one drives through the bushveldt of East Africa it is easy to spot herds of baboons grazing on the ground. But not by looking at the ground. Instead you look up and spot the lookout, an adult male posted on a limb of a tree where he has a clear view all around him — which is why you can spot him; he has to be where he can see a leopard in time to give the alarm. On the ground a leopard can catch a baboon. . . but if a baboon is warned in time to reach the trees, he can out-climb a leopard.
The lookout is a young male assigned to that duty and there he will stay, until the bull of the herd sends up another male to relieve him.
Keep your eye on that baboon; we’ll be back to him.
….
Take any breed of animal — for example, Tyrannosaurus rex. What is the most basic thing about him? The answer is that Tyrannosaurus rex is dead, gone, extinct.
Which brings us to the second fundamental question: Will Homo sapiens stay alive? Will he survive?
We can answer part of that at once: individually H. sapiens will not survive. It is unlikely that anyone here tonight will be alive eighty years from now; it approaches mathematical certainty that we will all be dead a hundred years from now as even the youngest plebe here would be 118 years old by then – if still alive.
Some men do live that long but the percentage is so microscopic as not to matter. Recent advances in biology suggest that human life may be extended to a century and a quarter, even a century and a half — but this will create more problems than it solves. When a man reaches my age or thereabouts, the last great service he can perform is to die and get out of the way of younger people.
Very well, as individuals we all die. This brings us to the second half of the question: does Homo sapiens as a breed have to die? The answer is: no, it is not unavoidable.
We have two situations, mutually exclusive: mankind surviving, and mankind extinct. With respect to morality, the second situation is a null class. An extinct breed has no behavior, moral or otherwise.
Since survival is the sine qua non, I now define “moral behavior” as “behavior that tends toward survival.” I won’t argue with philosophers or theologians who choose to use the word “moral” to mean something else, but I do not think anyone can define “behavior that tends toward extinction” as being “moral” without stretching the word “moral” all out of shape.
We are now ready to observe the hierarchy of moral behavior from its lowest level to its highest.
The simplest form of moral behavior occurs when a man or other animal fights for his own survival. Do not belittle such behavior as being merely selfish. Of course it is selfish. . . but selfishness is the bedrock on which all moral behavior starts and it can be immoral only when it conflicts with a higher moral imperative. An animal so poor in spirit that he won’t even fight on his own behalf is already an evolutionary dead end; the best he can do for his breed is to crawl off and die, and not pass on his defective genes.
The next higher level is to work, fight, and sometimes die for your own immediate family. This is the level at which six pounds of mother cat can be so fierce that she’ll drive off a police dog. It is the level at which a father takes a moonlighting job to keep his kids in college — and the level at which a mother or father dives into a flood to save a drowning child. . . and it is still moral behavior even when it fails.
The next higher level is to work, fight, and sometimes die for a group larger than the unit family — an extended family, a herd, a tribe — and take another look at that baboon on watch; he’s at that moral level. I don’t think baboon language is complex enough to permit them to discuss such abstract notions as “morality” or “duty” or “loyalty” – but it is evident that baboons do operate morally and do exhibit the traits of duty and loyalty; we see them in action. Call it “instinct” if you like — but remember that assigning a name to a phenomenon does not explain it.
But that baboon behavior can be explained in evolutionary terms. Evolution is a process that never stops. Baboons who fail to exhibit moral behavior do not survive; they wind up as meat for leopards. Every baboon generation has to pass this examination in moral behavior; those who bilge it don’t have progeny. Perhaps the old bull of the tribe gives lessons. . . but the leopard decides who graduates — and there is no appeal from his decision. We don’t have to understand the details to observe the outcome: baboons behave morally — for baboons.
The next level in moral behavior higher than that exhibited by the baboon is that in which duty and loyalty are shown toward a group of your kind too large for an individual to know all of them. We have a name for that. It is called “patriotism.”
Behaving on a still higher moral level were the astronauts who went to the Moon, for their actions tend toward the survival of the entire race of mankind. The door they opened leads to hope that H. sapiens will survive indefinitely long, even longer than this solid planet on which we stand tonight. As a direct result of what they did, it is now possible that the human race will never die.
Many short-sighted fools think that going to the Moon was just a stunt. But the astronauts knew the meaning of what they were doing, as is shown by Neil Armstrong’s first words in stepping down onto the soil of Luna: “One small step for a man, one giant leap for mankind.”
Heinlein based his definition of morality on the premise that without survival, there is no morality—if we do not exist, we cannot be moral; or to put it another way, morality becomes meaningless, if there are no moral agents to make moral or immoral decisions.
It’s not what most people think of when they ponder the concept of morality, but it does make sense. Though I don’t think it quite encompasses the entire concept, as people commonly use the terms moral and immoral to reference ideas, behaviors or situations that are not obviously bound up with survival (just think of prudish Christians declaring thong bikinis as “immoral” for example, and getting them banned from public beaches, as they have done in my area—it impacts survival hardly at all whether or not women wear thong bikinis, but certainly some people regard excessive display of skin as “immoral”).
Heinlein is also quoted as saying “The basis of all morality is duty.” But I don’t think that gets all of it either. I’d also add that morality is also based partly in empathy. We engage in moral behavior partly because, in pragmatic terms as I said, we have to form social groups and exist withing them, and in order for this to work we inevitably must devise moral laws that say murder, theft, rape, coercion, etc. are wrong. But part of how this works, biologically, is empathy—we feel empathy with other human beings, which causes us to feel guilty when we do them injury, or otherwise break these moral rules. Some individuals, such as sociopaths and psychopaths, lack the ability to feel empathy, which explains why they can so easily engage in immoral behavior.