The symbiotic nitrogen-fixing bacteria invade the root hairs of host plants, where they multiply and stimulate formation of root nodules, enlargements of plant cells and bacteria in intimate association. Within the nodules the bacteria convert free nitrogen to ammonia, which the host plant utilizes for its development.
In leguminous crop NFB act by nodule formation. In other non-legume crop these bacteria fixes nitrogen but not by nodule formation. For example Azatobacter chrococcum is found in large group of plant rhizopshere and known to help in nitrogen fixation.
To fix Nitrogen in root , the nitrogen fixing bacteria Rhizobium or Bradyrhizobium bacteria colonize the host plant's root system to form the bacterial nodules. The bacteria then fix the nitrogen required for the growth of the plant.
As mentioned by Madhukar Baburao Deshmukh , the most common root living bacteria is known as Rhizobium and this has several strains depending on their host plants . They remain in the root nodules of leguminous plants like pulse etc. and from there, they fix atmospheric nitrogen which benefits the host plants also . They are also known as symbiotic nitrogen fixing bacteria.
That is known symbiotic relatioship between plant and bacteria .Rhizobium ,nitrogen fixing bacteria settle at root,s plant through nodules then begun to fixing nitrogen which is useful for plant and that is only in leguminous
Regarding the last part of the question (How nitrogen is fixed), gaseous nitrogen is reduced by an enzyme complex called nitrogenase into ammonia which is then assimilated into organic form. This enzyme contains Fe/Mo cofactor and it is indeed composed of two proteins; dinitrogenase & dinitrogenase reductase.
Energetically, nitrogen fixation is costly. In fact, eight electrons and 16 ATP are needed for the procrss, six of them are used just for the reduction of N2 to NH3.
In its simple form, an electron donor such as pyruvate donates electrons to flavodoxin or ferredoxin, which in turns reduces dinitrogenase reductase. The later transfer the electrons to dinitrogenase one at a time and two ATP are consumed per electron transfer. Indeed, ATP hydrolysis at dinitrogenase reductase results in lowering reduction potential of dinitrogenase reductase allowing it to reduce dintrogenase. Reduced dinitrogenase ultimately donates the electrons to N2 at the active site of the enzyme resulting in NH3 formation.
Most of the legumes have symbiotic association with different species of Rhzobium. Roots of such legumes provide shelter and energy for multiplication of and nitrogen fixation by the rhizobium. Rhizobium has 22 or more nif genes including the main three structural genes (K, D and H) that leads to synthesis of nitrogenase, which mediates conversion of free atmospheric N into ammonia, that results in the formation of glutamine, asparagine, etc., ultimately making legumes the protein-rich crops.