The gases are produced as a by-product of digesting certain types of food. Flatulence producing foods are typically high in starch, and include beans, yams, sweet potatoes, chickpeas, citrus rinds, chestnuts, broccoli, cabbage, yeast in breads, etc.
In beans, the most notorious offenders in this regard, the problem seems to arise from starch molecules resistant to digestion: when the polysaccharides reach the intestines, intestinal bacteria feed on them, producing gas.
In the case of those with lactose intolerance (ie. most non-Caucasian humans), intestinal bacteria feeding on lactose can give rise to excessive gas production. Another source of excessive gas production in the stomach is stress. Stressful situations can cause the stomach to produce gas. Also, not only does tight clothing cause a gassy stomach to be painful, it also can contribute to the production of gas.

Many people notice that if they reduce their intake of most refined carbohydrates such as rice, pasta, potatoes, and breads, their stomach gas production decreases significantly.

Breastfeeding mothers are advised to avoid gas producing food in their diet because breastfed babies can vomit when there is too much gas in their stomach.

Methane is the primary gas released. Four other gases that are found in large proportion are nitrogen, hydrogen, carbon dioxide, and oxygen. As methane and hydrogen are flammable, some flatulence is as well. The gas released often has a foul odor which mainly results from sulfur compounds such as hydrogen sulfide that are the result of protein breakdown.