This issue 'stinks', but in the end it's something common to all: geniuses and dumb people, emperors and slaves.
When ingesting food and liquids, air enters your stomach. And if not by belching, then which is the other way to expel too much gas? (now I know why the Japanese do not belch much after eating).
But gases are also produced by our gastrointestinal flora and these are the cause of the stench odor. From blood some gases can also enter the gut and viceversa. Daily, about 7-10 liters of gases enter the gut, but about 1.9 liters are expelled through the anus as toots, 15 to 20 daily.
Most of the gases are ingested air, with 79 % nitrogen, the main one (78 %) and also air oxygen.
But in the thick intestine, the gut anaerobic (that are still present despite the lack of oxygen) bacteria produce methane (in some people only traces) and hydrogen, that's why the fart is flammable and can cause burnings in anus, rectum and buttocks if fired. Carbon dioxide is also present, produced by the air-consuming bacteria, brought by the blood.
The unbearable odor is induced by short fatty acids such as butyric acid (rancid butter smell), nitrogen compounds (skatole, indole) and sulfur chemicals, like hydrogen sulfide (which gives a rotten egg smell) and carbonyl sulfide. The fatty chemicals result from the activity of the bacteria, the other from protein decomposition (the last, of sulfur containing proteins) by the bacteria.
Because these activities take place in the lower intestine, it lasts for a few hours since the food digestion till the first "deliveries".
Incomplete digestion of digestible nutrients or indigestible food items means a feast for the bugs and many discharges of intestinal gas.
Aliments rich in some polysaccharides (sugars with big molecules), especially oligosaccharides (polysaccharides with a smaller molecule) like inulin and raffinose are your main embarrassing culprit. These are beans, milk, onions, radishes, cheese, cashews, Jerusalem artichokes, oats, yeast in breads, cabbage, broccoli, cauliflower, asparagus and other vegetables. The human body does not have enzymes for breaking them down and when we ingest them, we swallow fodder for the fart-producing bacteria. The resulting gases are mainly methane, hydrogen and carbon dioxide.
Some soak beans in the water 24 h before cooking them. This way, many fart-inducing sugars, which are soluble, go out of the beans into the water, but so do valuable minerals and proteins.
Lactose from unprocessed milk is another problem, aggravated when added to bread and cereal. In case of lactose intolerance, the person has very low levels of enzyme that allows the digestion of this sugar from milk. The result: booooom! A lot.
Fructose, a simple sugar abundant in onions, corn, wheat and sweet fruits (it is the main chemical, together with glucose, that give the sweet taste of the fruits) is harder to absorb by the gut and induces big flatulence (you know what happens when you eat too much pears, apples or plums...oh, yeah). Corn fructose is often used to sweeten soft drinks and fruit drinks, and juices also lead to flatulence!
Sorbitol, a simple sugar found in many fruits, is used as sweetener in "diet" and sugar-free foods, because it is not digestible. Indeed, by your gut, but your gut bacteria indulge in it, and, this way, sugar-free gum, candy, soda and so on make you a good methane-hydrogen producer...
Starches (like those from cereals, potatoes and beans), too, cause flatulence, except the rice. Cellulose fibers, good for gut transit, can be broken down to gases by gut bacteria.
Fats and protein also slow down the gut transit and bacteria have more time to produce gas from the sugars.
Aliments like beans and milk (rich in both sugars and sulfuric proteins), are not recommended if you want to avoid flatulence. Meat or eggs in combination with sugar-rich foods have the same effect.
In the end, each body is a universe and by trial and error you will find which type of food causes problems.
Anti-gas products like alpha-galactosidase (Beano) or lactase enzyme (Lactaid) are recommended if you want to avoid embarrassing moments and some simethicone (Gas-X) speeds the gas flow, fighting bloating.
Abundant flatulence can signal a gut problem, chronic irritation or other severe issues.