Any scientific technique that gathers information about something based on the kind of light it emits is called spectroscopy. Light -- usually from a laser -- is shone onto a given sample and scientists then measure how the light changes as it interacts with the sample.
Light travels in waves, and different colors of light have waves of different lengths. Red light waves are longer than blue light waves. White light is made up of all the colors mixed together. Atoms and molecules in any given substance produce, absorb or change light in very unique ways; it is like a chemical fingerprint, telling scientists which atoms and molecules are present.
There are many different kinds of spectroscopy, but most fall into the category of absorption and emission spectroscopy. In absorption spectroscopy, scientists shine infrared light on a sample and measure how much of the light emerges on the other side. In emission spectroscopy, they shine a lot of light onto a sample and then measure how much light bounces back from the sample. For example, when light hits molecules in cells, the cells absorb the extra energy and then re-emit light in varying colors. This is called fluorescence, and it can easily be detected and measured.
In the same way, astronomers can tell what chemical elements are present in stars. Astronomers pass starlight through a special instrument called a spectrograph, which separates the light into the various colors of the spectrum, like a prism. By studying how the spectrum changes, scientists can tell which chemicals make up the star, and even how hot it is, how dense, or how fast it is moving.