Cooperation and altruism are programmed into us because our genes are selfish. It sounds contradictory, but it's true. Looking at natural selection on the level of the individual simply can't explain altruism. Individual selection is a useful abstraction much of the time, but it it breaks down when you go too deep. It's a bit like how Newton's theory of gravity works quite well until you hit certain specific conditions, whereas relativity works in a much wider range of conditions but is too difficult to use most of the time.
If you look at selection from the point of view of genes and groups of genes, you see that altruism would arise. For example, consider the fact that any sibling of yours will share half of your genes. Your genes will 'want' him to survive almost as much as they want you to survive; genes that cause you to help a sibling will be favored- to a point, anyway. Selection would never favor genes that caused you to sacrifice your life for you brother, at least not directly. But what if sacrificing yourself could save three siblings? Between them, there would be a 150% chance of them sharing any given gene with you. From your genes' point of view, it is preferable that those three live, even at the cost of your life. Similarly, it would make good genetic sense to sacrifice yourself for three of your offspring, or six of your sibling's offspring. And sacrifices need not require your death; while it wouldn't be favorable for you to sacrifice your life for one sibling's, it might be favorable for you to spend a little time gathering extra food for him.
Now, through much of our species' history we have lived in relatively small groups. In these groups, there is a high chance that any given individual will share a fair number of genes with you. Perhaps your genes wouldn't want you to sacrifice your life for a cousin or nephew, but small acts of altruism would be favorable. In the present, of course, we live in cities, often surrounded by masses of people not remotely related to us, but as far as our genes are concerned everyone around us is likely to be a relative, so it is worth it to act altruistically towards them.