One of the most amazing things about astronomy is our ability to see inside the atoms that make up a star. We can't resolve the star into a disc, but we can peer into its inner workings like it was nothing!
It's like I'm looking through my telescope and see that there's a speck on a hillside. I know the speck is a person, but I can't tell if it's a man or woman, or if it has both legs, nothing. But I can read this person's mind!
Completely counter to what we'd expect, huh?
But how the heck is this even remotely possible? By spectroscopy, that's how.
Way back in 1814 Joseph von Fraunhofer, a German glassmaker discovered dark lines in the Sun's spectrum of colors. (He also discovered the diffraction grating and was the first to examine the spectra of stars, but that story is for another time.) It took till 1859 for Kirchhoff (and Bunsen of Bunsen burner fame) to figure out that these Fraunhofer lines (as they had come to be called) were a result of and depended on the energy levels of the electrons in the atoms of the material.
Kirchhoff discovered these 3 laws:
1) That a solid body (like a lump of iron) when very hot will produce a continuous wash of color in its spectrum.
2) That a hot gas will produce discrete lines of color at specific points in the spectrum, and
3) That a hot solid body surrounded by a cooler gas will show darker lines like the Sun does.
Item #2's lines are called an emission spectrum, and #3's lines are called an absorption spectrum because the one is emitted and the other is absorbed. Not only that, but the color position of the dark & light lines are the same when the gaseous element is the same:
Interesting, but in order to explain how it works we need to take a little trip. A trip into the world of the atom.
We like to think of the atom like a miniature solar system, with the nucleus of the atom being like the Sun and surrounding it the electrons take the place of the planets. In the solar system the planets approximately orbit in a plane we call the ecliptic. (Note the spelling there, it's not elliptic, it's ecliptic, with 2 "c"s.) In atoms the electrons don't have to keep to a flat plane but can orbit however they please, but you can continue to think of them being in a plane - it'll help.
Now in the solar system, if you were to fly a spaceship from Earth to Venus you would set out and presently you'd find yourself a quarter of the way there, then halfway, then 2/3 of the way and so on. The world of the atom, however, has a different set of rules called quantum mechanics that basically says that you can be at Earth or Venus and not in between. Yeah, it's weird, but that's how the universe is built.
These different orbits represent different levels of energy. So if an electron gains energy it can jump instantaneously from Venus to Earth. Likewise, if it looses energy it can jump from Earth to Venus. This is what is meant by a "Quantum Jump."
So how does an atom gain or loose energy? What does this have to do with spectra? Where you goin' with all this?
Here's how! Light! Yup, it's all done with light. A photon (a particle of light) has energy that is proportional to its color. A redder color equals a low energy photon and bluer color is a higher energy photon. In the case of the Sun (like #3 above) the Sun's continuous spectrum is partially absorbed by the cooler gas above the surface. There are photons of all energies in the continuous spectrum coming up from below. Those few photons that have the exact energy (the exact color) required to make an electron jump from one level to another are absorbed, making a dark Fraunhofer line.
It's that simple.
The atoms of cool gas strip out all the photons that have that particular color/energy from the continuum of colors, leaving a gap in the spectrum. That particular color is filtered out from the light coming up from the Sun's surface.
The hot gas in #2 emits lines as its electrons loose energy and drop from a higher energy orbit to a lower energy orbit. They loose the energy by emitting a photon of light whose color (or energy) is equivalent to the difference in energy of the 2 orbits, the ammount of energy lost by the electron. So the atom sits there, cooling down with the electron still at the higher orbit level until it is cool enough for the electron to exist at the lower level, then PING! the electron drops down (it makes that quantum jump) & a photon is emitted.
So simple! Scientists use the word "Elegant" to describe phenomena like this. Elegant. It fits.
So looking at a star's spectrum, even though you can't see the disc of the star, gives us a look inside the workings of the atoms in the star's atmosphere. This works for planets, too, when our robotic spacecraft go zipping by taking pictures. This is how we know what the volcanoes of Jupiter's moon Io are spitting out.
When the Hubble Space Telescope takes a pretty picture of a gas cloud or galaxy (or whatever) the public wants to see the visible light picture and go "Oooh!" but scientists, ever hungry for deep knowledge, ask for the spectrum. They want to know what they're really looking at and only a spectrum will do the job.
Spectroscopy absolutely rocks! It's a cornerstone of science.
And now you know how it works. Now you rock!