People fart in order for them to release gasses and avoid having poor digestion and pain. These gasses are byproducts of digestion. You can read all about it by going to http://en.wikipedia.org/wiki/Flatulence.
Flatulence occurs when a food does not break down completely in the stomach and small intestine. As a result, the food makes it into the large intestine in an undigested state. For example, if you are "lactose intolerant," it means that you lack an enzyme (lactase) in your intestine -- the enzyme that breaks lactose apart into two sugar molecules so they can enter the bloodstream. Without lactase, lactose passes undigested through the stomach and small intestine and arrives in the large intestine.
There, the lactose meets up with billions of hungry bacteria -- the natural "intestinal fauna" we all have in our large intestine. These bacteria are happy to digest lactose. They produce a variety of gases, in much the way that yeast produces carbon dioxide to leaven bread. Gases such as methane, hydrogen and hydrogen sulfide are common gases that these bacteria produce. Hydrogen sulfide is the source of the odor we associate with flatulence.