Nitrogen is a relatively inert diatomic (N2) gas, but also importantly, while there are other gases that refuse to react with much anything even more so, like e.g. Helium, Nitrogen is cheap since it's often a byproduct of industrial processes. It is also frequently readily available at scientific facilities in its liquid form for being an extremely effective coolant that doesn't damage electronic equipment or leave colored stains on optical equipment.
Since it's relatively light (atomic number 7 on the periodic table of elements) it will also expand pretty well in all directions and displace any impurities that might still linger suspended in air. It is also non-toxic, non-corrosive, non-hygroscopic, colorless, odorless, tasteless, and will displace Oxygen (O2) and greatly reduce fire hazard and oxidation.
I.e. it is the industry standard technique for the replacement of a hazardous or undesirable atmosphere with an inert dry atmosphere by displacement or dilution.
In case of the Hubble telescope however, the reason is also described in the same Wikipedia article a bit later on:
A shroud of multi-layer insulation keeps the temperature within the
telescope stable, and surrounds a light aluminum shell in which the
telescope and instruments sit. Within the shell, a graphite-epoxy
frame keeps the working parts of the telescope firmly aligned.
Because graphite composites are hygroscopic, there was a risk that
water vapor absorbed by the truss while in Lockheed's clean room would
later be expressed in the vacuum of space; the telescope's instruments
would be covered in ice. To reduce that risk, a nitrogen gas purge was
performed before launching the telescope into space.
This should also answer your last question regarding storing the Hubble Space Telescope in a vacuum chamber instead. While a vacuum chamber could prevent in-air impurities by simply sucking all the atmosphere out of it, that process wouldn't clean it of possible water molecules gathering on and around its hygroscopic multi-layer insulation, if they started condensing. It is also pretty hard to find a vacuum chamber large enough to hold the whole telescope, and a malfunction of its pump could result in a sudden gush of air, damaging the telescope and its shroud.
In contrast to a vacuum chamber, using Nitrogen only results in a slightly changed atmospheric pressure when the clean room environment it was stored in would be accessed by the staff working on it. They also wouldn't need expensive and cumbersome pressure suits, and any Oxygen supplied Hazardous Environment (HEV) suits would do just fine for such tasks.