Legumes are very healthy foods. Humans (to different extents) lack the enzyme needed to digest the complex carbohydrate raffinose and this is the cause of the problem. Just like some people lack the enzyme needed to digest lactose.
from http://www.niddk.nih.gov/health/digest/pubs/gas/gas.htm
Most foods that contain carbohydrates can cause gas. By contrast, fats and proteins cause little gas.
Sugars
The sugars that cause gas are: raffinose, lactose, fructose, and sorbitol.
Raffinose
Beans contain large amounts of this complex sugar. Smaller amounts are found in cabbage, brussels sprouts, broccoli, asparagus, other vegetables, and whole grains.
Lactose
Lactose is the natural sugar in milk. It is also found in milk products, such as cheese and ice cream, and processed foods, such as bread, cereal, and salad dressing. Many people, particularly those of African, Native American, or Asian background, have low levels of the enzyme lactase needed to digest lactose. Also, as people age, their enzyme levels decrease. As a result, over time people may experience increasing amounts of gas after eating food containing lactose.
Fructose
Fructose is naturally present in onions, artichokes, pears, and wheat. It is also used as a sweetener in some soft drinks and fruit drinks.
Sorbitol
Sorbitol is a sugar found naturally in fruits, including apples, pears, peaches, and prunes. It is also used as an artificial sweetener in many dietetic foods and sugarfree candies and gums.