When an electron in an atom drops from one energy level to another, it gives off a specific amount of energy. It turns out that each element in the periodic table gives off energy at a unique wavelength of light. If you are able to measure light waves across the entire range of the electromagnetic spectrum, you could detect the presence of any element.

Keep in mind that an object doesn't need to be a bright star, to give of energy. All objects made of matter produce energy waves around them. Humans produce infrared waves, for instance. Even rocks produce energy waves. That's how rovers on Mars are able to determine the contents of the rocks they pick up, without sophisticated chemistry labs. So if you could capture energy waves coming from a planet, you could determine the composition of that planet.

Sources:

Astronomy Lecture. YouTube. Dr. Dave, 13 Jan. 2007. Web. 30 Jan. 2013.

Image Source: http://www.astro.washington.edu/courses/astro211/Lectures/hydrogen_atom.html