Spectroscopy (ENGLISH) Espectroscopía (ESPAÑOL)
Light can be spreaded in colors using a prism or a diffraction grating. White light is made out of all colors. We can do that with any light source, even stars, galaxies, comets, etc.
All matter is made of atoms such as hydrogen, helium, iron, etc. Atoms have a nucleus surrounded by electrons in orbit, which can adopt different configurations, depending mainly on the distance to the nucleus, but also the shape of their orbits. Interestingly, for any given element, say hydrogen, with just one electron, the electron orbit can only adopt a specific set of configurations, not any one. Therefore, when atoms absorp or emit light, and consequently lose or gain energy, they do so at specific colors, particular for each element. Two examples are shown below (Helium and Sodium, images from wikipedia).
Helium 
Sodium 
That way, we can identify the presence and proportions of elements in very distant objects, from their light. This is how astronomers determine the chemical compositions of stars or any other astronomical object.
For more information, follow this link to wikipedia.