Consider that light is nothing more than electromagnetic radiation; it is generated or absorbed only by changes in electromagnetic potential.
Now also consider that electron orbitals in molecules are discrete and quantized: you can be in this orbital (with such-and-such potential energy) or in that orbital (with a slightly different potential energy) but you cannot exist between those two orbitals.
Therefore, light can only be absorbed or emitted by a molecule if it can precisely force an electron into a different orbital, which in classical terms means that it only works if light has a particular frequency.
As you might imagine, sticking protons (which generate electron fields) at different positions within a molecule, or equivalently adding protons to an atomic nucleus, will change the permissible orbitals very slightly. Consequently, the light that an atom or molecule can emit or absorb will change as well.
Stuff being in the way will of course diminish the signal (by direct absorption or by scattering it). But space is pretty empty. The regular air on Earth contains ~10^19 particles per cm³, and it can take several km of air to start seriously interfering with light. A dense portion of space contains something like 10^4 particles per cm³, and a less dense region maybe 10^-4 particles per cm³. So, once you leave the atmosphere, the noise that's destroying the signal is simply far, far, far weaker than the signal.
Now for your first question. As I mentioned in my first comment, the spectral lines arise from different orbitals. What this means is that there isn't a single line associated with a molecule, but actually several lines. So if you think you see a line from one molecule, you should be able to find the other associated lines, and the more of the lines you can find, the stronger the evidence.
As a couple of people have noted, this isn't a perfect process, so you can get into arguments if your identification is actually correct or not.
To wit, considering how ubiquitous camera sensors are, you'd think that we'd have the ability to do spectrosocopic analysis on our smart phones with a simple/cheap adapter by now.
I am fairly certain that this does exist, I remember almost purchasing a spectroscopy adapter for my iPhone way back around 2014. I didn't buy it and I guess it wasn't too popular.