I really wanted to give a good answer to this, but I don’t know enough about it! Here’s what I can work out from a bit of (rather yucky) research:
The *reason* we ‘fart’ (I don’t know what a better word to use is, so I’ll use yours) is because there’s gas inside us that needs to get out (otherwise it would get very uncomfortable indeed!) This gas comes from air that we suck in while eating (close your mouth when you chew! etc. etc.), but also gas that is released through digestion. When your body digests stuff, lots of chemical reactions occur in the stomach. Lots of these produce gases as a result – and it all has to come out somewhere, unfortunately!
There are also bacteria inside our guts that produce gases as they make their food.
So for example, when we eat baked beans there are some sugars that we can’t digest. These sugars make it down to near the end of our intestine where certain bacteria live. The bacteria ferment the sugars and this fermentation process creates gases.