The stomach is an organ found in almost all vertebrates. Although there is considerable variation among the different groups, and there are some fish that don't have one at all, in general it has two functions. Firstly, it helps to store food for later digestion so that you don't constantly have to be eating, and secondly, it begins the digestion of food both by physically grinding it up and by mixing it with acid and digestive enzymes. Sometimes these functions are separated to some extent - for example, birds have a large crop for food storage, and a smaller true stomach below it that digests the food, and includes a muscular, grinding gizzard (a useful thing when you have no teeth). But the most complex stomachs of all are found in mammals.
A cow does, indeed, have four stomachs. Or, at least, it has a stomach divided into four separate chambers, which amounts to the same thing. Nor, of course, are cows alone in this. It's a feature found throughout the cattle family, which is a fairly large group consisting of over a hundred species - most of them antelopes, although it also includes the sheep and goats. The cattle were not the first family to evolve the feature, and we know that because its also found in all their close relatives, including such animals as deer and giraffes. In fact, the only truly cloven-hoofed animals that don't have four stomachs are the pigs and peccaries - which is why they aren't kosher.
In fact, we can group all the mammals that have this four-chambered stomach together. This group are called the "pecorans". Why not just "ruminants"? We'll get onto that later.
Animals with four-chambered stomach in blue. For the sake of simplicity, the above chart excludes the chevrotains, which do have the four-chambered stomach, but probably branch off even before the giraffes. For the same reason, I have also grouped multiple related families together. For example, peccaries are grouped in with pigs (since they probably have a common ancestor) and pronghorns in with giraffes. And the "etc." for hippos is a surprisingly large one that I'll leave for another day!
So, why bother with four stomachs, when most other animals do perfectly well with just the one? In a word: grass.
Well, not just grass, although that is the major culprit; many other leaves also play a part, as is obvious from the wide range of different animals included in the Pecora. One thing they do all have in common, though, is that they are all herbivores. The problem with being a herbivore is that many leaves, and grass in particular, are not especially digestible or nutritious. You have to eat a lot of them to get enough nutrients, which is why big, herbivorous animals spend a lot more time grazing than animals like lions do hunting.
If you really must eat plants, the easiest option is to eat fruit, or at least soft, young leaves, and there are a number of mammals that do that, especially among primates. But there is only so much fruit to go round, especially if you aren't a forest-dwelling animal, so the big grazing animals had to come up with some way of extracting the maximum amount of nutrition from the relatively poor quality food they are left with. Which is where the four stomachs come in.
The first stomach - or first chamber - is the rumen. This is a large chamber, often with a rather complex shape, that helps to store the food after it is eaten. It has no digestive lining, and strictly speaking, like the crop of birds, is just an extension of the oesophagus, or gullet. However, while it can't digest food itself, it does contain numerous bacteria, protozoans, and even some fungi, all of which help to ferment the food, so that that the animal can digest it later on. This fermentation produces a lot of gas, and ruminants do, indeed, belch a lot of methane as a result.
The liquid resulting from this process trickles down into the second chamber, the reticulum, where further fermentation occurs, but the solid food is partially regurgitated up into the mouth for a second round of chewing. This, of course, is "chewing the cud", and it helps to further break down the food. The reticulum is a spongy organ, but otherwise very similar to the rumen, and is the source of the food known as honeycomb tripe. The chewed cud, along with the fermented liquid, however, then passes straight on to the third chamber, the omasum.
Like the first two chambers, the omasum has no digestive glands, and it mainly serves to continue to mash up the food between its many folds. It probably also absorbs at least some of the nutrients that have already been released by fermentation, but it is the smallest of the three chambers, and soon passes the food on to the fourth and final chamber, the abomasum. It's here that the digestive glands are located, secreting acid and enzymes to break the food down, just as the stomach of any other mammal does. Indeed, the abomasum is the only "real" stomach; everything else is just part of the oesophagus. From here on, digestion is fairly normal.
It may not have escaped your attention, however, that, wide though the group of "pecorans" is, they aren't the only large grazing animals. Pigs aren't really grazers, they get by by eating a wider range of more digestible food, but what about camels or horses, for example? If they don't have four-chambered stomachs (and they don't), how do they extract nutrients from grass?
Well, firstly, the pecorans are not the only ruminants. Camels, llamas, and their kin also chew the cud, and they have a three-chambered stomach that does look rather like that of cattle, only without the omasum. Again, only the last chamber is the real stomach, and this similarity meant that, for a long time, it was thought that the pecorans had evolved from camel-like animals by the addition of an extra chamber between the reticulum and abomasum. But we now know that can't be right, because pigs and hippos - which have a relatively normal stomach - are closer to cattle than they are to camels. Which means that camels evolved their three "stomachs" entirely separately from the pecorans evolving their four.
It is viable, however, to ferment the food without chewing the cud. Kangaroos do this; they have a really large stomach, suitable for fermentation, but it only has one chamber, so they can't separate the cud out to chew it - which is less efficient, but somewhat quicker than, the ruminant process.
Horses, however, take a completely different approach. Here, the large intestine is greatly expanded, and contains fermenting bacteria, so that it can act just like the first two chambers of the ruminant stomach. Indeed, this structure is usually much bigger than the stomach itself, just as the fourth chamber of the pecoran stomach is smaller than the first one. We see this hindgut fermentation system, not just in horses, but also in rhinos, tapirs, koalas, rabbits, and a whole host of other herbivores.
Having the fermentation chamber positioned behind the stomach makes chewing the cud impossible, but it does mean that the animal can process food more quickly, and that it's already partly digested by the time fermentation begins, which does have some advantages. On the downside, the very fact that it's quicker means that it isn't quite as efficient as rumination, and that you do have to keep eating to keep food moving through your digestive tract.
A way to get the best of both worlds is, rather than passing fermented food back up to the mouth from the stomach, to ferment it in the hindgut, pass it out through your back end, and then eat it again. It's much the same idea as chewing the cud, if rather less pleasant from a human perspective, and it is, of course, what rabbits do.
Humans only have one stomach, and no large fermenting chamber in our hindguts. We are not herbivores; if anything, apart from our teeth, our digestive system looks more like that of a pure carnivore than that of an omnivore. Yet, obviously, we can survive perfectly well on a vegetarian diet. This is because we, unlike any other animal, have invented cooking. If you tried to survive on a diet of nothing but uncooked leaves, nuts, and so on, you would quite possibly die, and you certainly would if you tried to live off grass.
Having said that, the fact that humans did not evolve to be herbivores is a pretty poor argument against vegetarianism. We did not evolve to live in houses, either, and yet we do that. We are sentient beings, capable of choosing how we live, and we don't have to be slaves to our evolutionary history if we don't want to be. But that's a whole different debate.
[Picture from Wikimedia Commons. Cladogram adapted from Price et al, 2005]