undigested food rots somewhat on the way through and bacteria eats it and makes more gas. this is particularly true with carbs that don't digest or human-indigestible carbs (sugar alcohols for example). if it's fully digested (and absorbed) it's mostly fibre coming out. I had massive problems (celiac) about 20 years ago with digestion, went to a doctor specializing in it.