Beans and other legumes are well known for the production of flatulence (intestinal gas) as a side effect. The cause of this gas is a class of carbohydrates called alpha-galacto-oligosaccharides, see image below. The most common carbohydrate of this class is raffinose (also known as melitose).
Beans contain large amounts of raffinose, smaller amounts are found in cabbage, brussels sprouts, broccoli, asparagus, other vegetables, and whole grains.
Raffinose and related carbohydrates (Source)
Raffinose is non-digestible in the human upper intestinal tract. This means that this carbohydrate reaches the large intestine. In the large intestine raffinose is rapidly fermented by the intestinal bacteria. Unfortunately, some of the end products of this fermentation are gasses such as carbon dioxide and hydrogen. These gasses accumulate in the intestine, which causes a higher pressure in the intestine (bloating) and will leave the body as flatulence. Some part of the gasses are absorbed into the blood and excreted through the lungs.
Both the absorbance of gas and the composition of intestinal bacteria vary widely among people. Due to these variations some people experience more gas after ingesting beans than others.