So how do we know that the sun is made mostly of Hydrogen, Helium and a handful of other elements? Astronomers use a technique called spectroscopy and here is how it works.
White light really consists of all the colors of the rainbow. You can make the colors appear by shining a light through a prism (just like the cover of that Pink Floyd album). Astronomers use a device called a spectrograph which is mounted on a telescope and has the ability to split light into all the colors of the rainbow. What's the purpose of all this? The sun generates white light in its core as Hydrogen fuses into Helium. Each light particles (called a photon) has a slightly different amount of energy. This is what actually causes the rainbow. Red light has a certain energy, while blue light has a little bit more energy, and we perceive this energy difference as a difference in color.
The chemical elements within the sun are made of protons, neutrons, and electrons. Thinking back to your chemistry days, the electrons orbit the outside of the atom, while the protons and neutrons make up the core or nucleus. These electrons sit in "energy levels" and light (photons) have the ability to excite these electrons to higher energy levels. When a photon hits an atom, if that light has just the right amount of energy, the electron will absorb the photon and be "excited". Electrons around different elements require different amounts of energy to get excited.
So here's the connection. If the Sun is made of lots of Hydrogen, then colors of light that excited Hydrogen electrons will be absorbed by the Hydrogen atoms in the sun. So when we look at the Sun's spectrum (above) we see black lines where color has been absorbed by different elements. These are called absorption lines. In chemistry labs we can see what absorption lines Hydrogen, Helium and other elements produce. Then when we have a spectrum from the sun, we can look for specific absorption lines from different elements and decipher what the sun is made of! Spectroscopy is used to figure out what stars are made of, along with the atmospheres of planets, and even giant clouds of gas in outer space! The image above is the entire visible spectrum of the sun. We see many absorption lines caused by Hydrogen, Helium, and other heavier elements.