Think about how light passed through a prism splits from white light into its rainbow colored components. It's the exact same thing. All you need to do is see what's missing, and you can discern the elemental composition of the atmosphere[s] that said light passed through. And it all started with Newton playing with a prism, and thinking beyond 'my, what pretty colors.'
The really cool thing about this is that it also can tell you some things that defy 'common knowledge.' For instance the Moon actually has a persistent atmosphere, and it's made out of sodium! It's exceptionally thin, but it's there - and can be picked up by spectroscopy.
[1] - https://en.wikipedia.org/wiki/Astronomical_spectroscopy
what gets me isn't the lack of complexity, but the lack of light. holding a prism to capture the sun's light or a flash light or whatever is simple since there's so much of it. even in telescopes, stars are mere pixels.
I will never not be amazed that we can read the atmospheric spectra of exoplanets.