Throughout the 1800s and early 1900s scientists had the tools to make some pretty sophisticated measurements on light. For example, they could put light through a prism or bounce it off a grating and split incoming light into all its colors. They would end up with a picture of the intensity of the light source at all different colors. That spread of colors is called a spectrum, and the scientists who examined those spectra were a bit confused by the spread of colors they saw. The first decades of the 1900s saw a big leap in understanding. Scientists now understand how spectroscopy can be used to identify elements and compounds.
Light contains energy. If an atom has extra energy, it can get rid of it by sending out a little packet of light, called a photon. It also works the other way around: if a photon comes near an atom that could use some extra energy, the photon can be absorbed by the atom. When scientists first started accurately measuring spectra, one of the things that confused them was that many spectra were discontinuous. That is, when sodium was burned, its spectrum wasn't a smooth spread of yellow light -- it was a couple distinct, tiny bands of yellow. And every other atom is the same way. It's as if the electrons in the atoms could only absorb and emit a very narrow range of energies -- and that turned out to be exactly the case.
The discovery that electrons in an atom can only emit and absorb specific energy levels is the heart of the field of quantum mechanics. You can think of this as if an electron is on a kind of ladder around the nucleus of its atom. The higher on the ladder, the more energy it has -- but it can never be between the steps of the ladder, it has to be on one step or another. Those steps are called energy levels. So, if an electron is in a high energy level it can get rid of extra energy by dropping down to any of the lower levels -- but not anywhere in between.
An atom stays together because the nucleus in its center is positively charged and the whizzing electrons are negatively charged. Opposite charges attract each other, so the electrons would tend to stay close to the nucleus. But the strength of the pull depends on how many positive charges are in the nucleus, and on how many other electrons are whizzing around, kind of blocking the outermost electrons from feeling the pull of the positive nucleus. So the energy levels in an atom depend upon how many protons are in the nucleus and how many electrons are orbiting the nucleus. But when an atom has a different number of protons and electrons it becomes a different element.
Because each element has a different number of protons in the nucleus, the energy level of each element is unique. Scientists can use this information in two main ways. First, when a substance gets extra energy -- such as when you put salt in a flame -- the elements in the substance will often get rid of that energy by emitting light, called an emission spectrum. Second, when light travels through a gas, for example, the gas can absorb some of that light -- that's an absorption spectrum. In emission spectra, bright lines will show up corresponding to the difference between energy levels of the elements, where in an absorption spectrum, the lines will be dark. By looking at the pattern of lines, scientists can figure out the energy levels of the elements in the sample. Since every element has unique energy levels, the spectra can help identify elements in a sample.