Spectroscopy is one of the most useful tools of modern astronomy. With it we can identify the atomic and molecular composition of celestial objects, we can measure the relative motions of stars, and we can observe the expansion of the cosmos. While early spectroscopes used prisms to break light into a spectrum, modern telescopic spectroscopes generally use a device known as a diffraction grating.
A prism is able to separate light into colors due to the fact that different colors bend at different angles when passing through a material. You might remember this effect as the cause of chromatic aberration in telescopic lenses. A diffraction grating uses the the wavelength of the light itself to create a spectrum.
Since light has properties of a wave, it spreads out in all directions from a source, just as ripples caused by dropping a stone in a pond. Just as water waves can interfere and overlap, so can light waves. The resulting effect that we see is due to the sum of all the light waves. A diffraction grating is a fine grating of reflective surfaces, kind of like a reflective picket fence (but tiny). When light strikes the diffraction grating, it is reflected from each different grating. The light from each of those reflections spreads out in all directions, and they all interfere and overlap each other. Because of the spacing of these gratings, different wavelengths of light are favored at different angles. At each different angle of reflection we see a different wavelength.
There is a specific mathematical relation between the angle of reflection and the wavelength, so with a good diffraction grating we can get a very precise measure of the wavelength. This makes it possible to make precision measurements of starlight and compare them to measurements done in the lab. From this we can determine the properties of stars and galaxies.