There are many ways to do spectroscopy because of the wide range of wavelenghts of light. I won’t go into detail, but essentially what spectroscopy does is either:
- Put energy into a sample and see what is absorbed (absorbtion spectroscopy)
- Put energy in a sample and see what comes out (emission spectroscopy)
The reason those two methods produce characteristic results for each element is the following: An atom is made up of a nucleus of a certain charge and electrons canceling that charge around it. Those electrons are confined to so-called orbitals due to quantum weirdness (the “quantisation” of the orbitals is literally the origin of the word quantum). Those orbitals have different energies (you can imagine that an electron being very close to the nucleus is more strongly attracted than an electron which is farther away).
Because the electrons need to always be on those orbitals with fixed energies, only certain energies of photons can interact with them (if a different energy photon wanted to interact with an electron it would need to push the electron “between” two orbitals which is forbidden by quantum mechanics)
So now only certain energies of photons (which relate directly to wavelength) are absorbed, the rest passes uninterrupted leading to bands in the spectrum where lots of photons are absorbed.
Now depending on how many electrons your atom has and how far away they are from the nucleus those absorbtion bands will vary, giving you a good idea which atom you are looking at.
Emission spectroscopy works the other way around, instead of you seeing what is absorbed, you randomly put energy (often using heat) into the atom. When the atom wants to go back to its most stable state it has to emit a photon, this photon needs to correspond to a gab between two orbitals (because else the electron either starts or ends outside of an orbital (which is forbidden))
Here the Research Gate link if you want to check out the full extent of this XD http://dx.doi.org/10.13140/RG.2.2.30887.09128