The plants are green, so can make their own sugars via photosynthesis (make food from the sun's energy). But the bacterium is not green, and cannot make its own sugars, for energy, as usually only green plants can photosynthesize. Fortunately, the bacterium can do some chemical magic, and change nitrogen found in the air into a simple nitrogen compound that the plant can use, such as nitrate. Simply, the plant gives the bacterium some sugars, (energy food to do their chemical magic ) and the bacterium gives the plant some nitrogen, and everyone is happy. A fair trade.
Some nitrogen-fixing plants do not form nodules on their roots, and they are considered by researchers to be more primitive, and fix lesser amounts of nitrogen.