One tool that astrophysicists use is a graph of the number of photons that hit the detector from a source as a function of the energy of those photons. Such a graph is called a spectrum (the plural of spectrum is spectra).
What good are spectra? The electrons in an atom can only assume discrete energy levels, so that only a photon of a specific energy can excite the electron into another energy level. For example, in hydrogen's ground state, it takes a photon with 10.2 eV of energy to excite the electron into the next higher state - a photon with 10 eV of energy will do nothing, as will a photon with 10.5 eV of energy.
Going in the other direction, when an excited atom de-excites, it will emit a photon of a specific energy - the same energy it would have taken to go from the de-excited state to the excited one.
So, if we know the energies needed to excite various elements to different excited states, we can look for a bump (emission line) or a deficit (absorption line) in the spectrum at those specific energies. The image below shows the same spectrum that was at the beginning of this page, but with the addition of an absorption and an emission line. The blue line shows what the spectrum would look like without these spectral lines.
Let's think about a couple of examples of how these lines might arise.
Suppose there is a star emitting light at all energies isotropically (in all directions). Between Earth and the star lies a cloud of hydrogen. Since the star is emitting light at all energies, some of those photons will have an energy of 10.2 eV. Those 10.2 eV photons will get absorbed by the hydrogen cloud. A hydrogen atom won't stay excited for long and will re-emit that 10.2 eV photon. However, the atom will emit the photon in a random direction, so some of the 10.2 eV photons that were heading toward the Earth will be emitted in a different direction.
Then, when we look at a spectrum from this star, we'll see a typical spectrum of a star but with a dip at 10.2 eV.
Now image a different star with a cloud of hydrogen that is not between us and the star. Again, the star is emitting light at all energies isotropically, so some of the 10.2 eV photons it is emitting will run into the cloud and get absorbed by the hydrogen.
Again, the hydrogen will de-excite, emitting a 10.2 eV photon in a random direction. Some of those photons will be directed toward Earth!
Then, when we look at a spectrum from the direction of the cloud, we'll see a spike at 10.2 eV.
These examples are fairly simplistic. In reality, hydrogen has a number of levels (infinite, really, but signatures from the levels above the first few are generally smaller than we can measure), so there would be more than one line observed. In addition, once a hydrogen level is excited, it doesn't have to return directly to the ground state; it could, for example be excited to its 2nd excited level with a 12.1 eV photon, then de-excite first to the 1st excited level emitting a 2.1 eV photon and finally return to the ground state emitting a 10.2 eV photon.
On top of that, hydrogen is not the only element in the Universe, so we usually see lines from other elements. In fact, one important element for X-ray astronomy is iron.