Nitrites are formed in a couple of different ways
1. Nitrates in the soil are absorbed by plants and then reduced from nitrate to nitrite before further reduction to ammonium (NH4+) which is what is used by the plant.
2. Nitrites are also formed as an intermediate in the formation of nitrates by the bacteria. Ammonium uses one type of bacteria to convert to nitrite and another type of bacteria continues the conversion to nitrate. High levels of nitrites are toxic to plants so they must be converted to nitrates for the plant to survive.
A similar process can also happen in the ocean in the marine nitrogen cycle. Bacteria present in sea life can also convert ammonium to nitrite and then to nitrate.