One reason for gas formation is the inability of your gut to completely digest the food eaten by you. In your small intestine reside several types of bacteria. The gut bacteria consume the undigested food present in your small intestine and give off the gas during the metabolism process.
Some people may have a leaky small intestine and have trouble absorbing the nutrients. The unabsorbed nutrients may leak out and be acted upon by the bacteria in the gut.
Also if your large intestine is not able to properly clear out your stomach wastes then waste matter may rot inside your gut and cause gas as well as make you diseased.