The article How Food Works has generated a lot of questions, many of them a bit embarrassing. Take this one, for example. But this question is so common that it seems it is time to explore the world of flatulence. We all suffer from this problem to varying degrees. Where does the gas come from?
In How Food Works, there's a good discussion of the digestive process. One thing that is obvious in the article is that digestion involves "breaking things down." Everything in food has to be broken down into small units in order to enter the bloodstream. Protein must be broken into its individual amino acids, fats must be broken into fatty acids, and carbohydrates (both simple and complex) must be broken into individual glucose (or equivalent) molecules.
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 (see How Bread Works for details on yeast). 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.
Certain foods produce more flatulence than others because they contain more indigestible carbohydrates than others. Beans, as you might expect, are particularly well-endowed in this regard.
Here are some interesting links: