I've heard a few times that adding a pinch of baking soda to the water you use to soak beans/lentils in, reduces the gas it makes you have. I've not do any measured experiments myself, but I have anecdotal evidence it does help. But what's the science behind it?
The actual action of soaking is what does most of the work.
Most legumes have complex oligosaccharides, a type of complex sugar. Digestion of this complex sugar is what causes flatulence. By soaking your beans will help remove some of this excess sugar. Be sure you discard the soaking water.
Though it is often said that adding baking soda helps I've yet to see any verifiable evidence that it would help. Also, don't forget that baking soda isn't tasteless, it could easily add a salty or soapy flavor to your once pristine beans.
In addition to what rheone said, I have noticed that using baking soda kind of softens food. A characteristica example of this is
which isn't the best solution because just a minute is enough to miss it and eat a soup instead of green vegetables.
I have also noticed that if you use soda in your beans overnight, then you are easily able to peel them! The reason for this is that soda really softens them, beacause it interacts with peel in a way that it makes peel's pores bigger which makes easily for the oligosaccharides to move out, due to difference in concentration.
Baucause saccharides in general are long molecules we have to help them get out, so a good solution is to make room for them!