You walk alone into a restaurant in some place where you know for a fact that you will never return to. The people working there are strangers to you, and you know you will never see any of them again. You have your meal, and it was alright. You get the bill and, this being America, you know you are expected to pay a tip (15-20% of the bill is commonly expected - this is why math is taught in school). As you are about to leave a tip, you wonder why you would. It strictly speaking isn't required. If you don't pay the bill the restaurant could call the police on you. But not if you don't leave a tip (did you know that TIP
The idea here is that we leave it because we otherwise somehow feel bad about not doing so. We feel that it's wrong not to tip. But why is that? Why not, in the above scenario, rationally arrive at the conclusion that there is no reason to, since whether you do or not will never affect you personally. You won't even be served by the same staff, so they can't punish you if aren't going to be nice. We do so because our morals tells us that we should, and if we act against that, then we feel guilty. Or, we feel guilty if we don't, so we do. But why do we feel guilty?
Recall that you are alone, and so there is no one with you that you can gain from making a good impression on. If you don't pay a tip, no one that will ever meet you again, or who will communicate with anyone you will ever meet again, will know the difference. So, evolutionarily speaking, what's the advantage of leaving a tip, and why do we have a sense of guilt when we don't tip?
One explanation is that we evolved under conditions where breaking the moral codes were always punished, even if just in a minor way, when the cheater was found out. Humans, or pre-humans, or however far back one thinks we have to go, lived in groups where everyone knew each other, and so the risk of meeting whoever we didn't 'tip' again was always present. Our feelings of guilt thus did not have to take into account that we might one day meet someone whom we would never ever meet again. Only in present day society does this ever happen to us, as when we travel coast to coast and pass by Guymon, Oklahoma and stop at a local diner. In other words, we can think rationally that there is no reason we should feel bad for ourselves when not leaving a tip, but we do so anyway, because guilt is a feeling we don't arrive at rationally, and our emotions don't know that we will never come back to Guymon and propose to that waitress.
Another explanation is more down to Earth. We feel guilty about not tipping because such guilt ensures that we do tip, and therefore stand a better chance at having sex with the waitress, or not getting into a deadly fight with the waiter. Both of these are very important things to consider in evolution, as in daily life. I call this explanation more down to Earth because it doesn't need to invoke the somewhat questionable evolutionary psychology kind of reasoning, like "this evolved at the dawn of humanity", the way the previous explanation does. Also, both of these have happened to me: I didn't pay a tip, and the waiter followed me into the street and gave me a good lecture in the middle of the sidewalk in front of everybody. I also have tipped a waitress more than 20 percent, and while I didn't get to have sex with her, I'm sure my chances were better than if I had tipped her just 15 percent.
It is also possible to argue that we feel guilty when we don't tip because we have learned that we must. In this way guilt is based on culture, and not on genes. Obviously, there is no genes for feeling bad when not tipping. I grew up in Denmark where people don't have to tip, and when they do it is as good as never in the 15-20 percent range. Still, when I moved to New York I quickly learned how to tip, and to feel bad when I didn't. In the middle of the sidewalk I defended myself saying that the food was a catastrophe, but the waiter was right that I could just have told him so, and he would have taken the food back to the kitchen, and I would not have been billed. But the point here is that even if one argues that feeling guilt in this situation is learned, as I learned it, the fact that we feel guilt when we err is not learned, but is an instinct that evolved because it serves a purpose. This third explanation then isn't really contrary to either of the first two, and I mention it only because everything comes in threes.