Cows and sheep chew their cud because they are classified as ruminants, which means that they cannot properly digest food the first time. They must regurgitate the food and chew it again several times until it is broken down enough to go through the rest of the digestive system. During the process of rumination, various micro-organisms from the cow or sheep's gut are also regurgitated, and this helps break down the plant matter further.
"Cud" is what the regurgitated food is called. Ruminant animals can spend hours each day chewing their cud. This also allows the saliva to work on breaking down the food before it hits the intestines.
Ruminant animals have four-part stomachs instead of one chamber like humans have. The four chambers are called the reticulum, rumen, omasum and abomasum. These four parts work together to process the food, all with a different specific function. The reticulum takes the liquid portion of the food and passes it on to the rest of the digestive system. The rumen catches the solid parts and sends it back up the esophagus to be chewed again. The omasum takes the food that has been regurgitated and chewed enough to pass and absorbs the water and nutrients from it. The abomasum is the only part of the ruminant's stomach that chemically digests the food.