The food we eat is broken down into its simplest parts for digestion- carbohydrates into glucose, protein into amino acids etc. Once those nutrients have been absorbed we are left with the waste product faeces.
Although this waste product is now useless to us and is making its way through the bowels to the rectum, there are billions of bacteria inside our gut that feast on the faecal matter. Just as humans use glucose in respiration creating water and carbon dioxide (gas) as waste products; so too do the bowel bacteria create gaseous waste products as part of their metabolism.
The main gas waste products are hydrogen and methane. The only way for the gas to escape the bowels is to come out the other end as a fart. Depending on what we eat depends what waste products are left over in the gut. Some foods will be metabolised by bacteria into sulphur gases which makes farts smell the worst.