How Do We Know What Stars Are Made Of?
For most astronomical objects, stars included, all we get from them is their light. Fortunately, there is a lot of information in that light. Isaac Newton first noticed that white light can be broken down into a range of colors that are familiar to all of us, the colors of the rainbow. He also found that these colors can't be broken down further, they are fundamental. Each spectral color of light corresponds to light of a particular frequency. In the work the won him the Nobel Prize, Albert Einstein showed that the frequency of light is directly proportional to the energy that light imparts to a substance when it is absorbed or removes when it is emitted. We now know that these distinct colors correspond to changes in the energy state of the electrons in the atoms. Since those energy states are unique to each type of atom the set of differences is also unique.
As early as the 1750s it was noticed that when certain substances were heated they glowed with a different color. When this light was analyzed by separating it into a spectrum it was seen that each element has a distinctive set of colors of light that it emits. Here's the pattern produced by the elements hydrogen and helium when heated enough to make them glow.
Hydrogen Emission Spectrum with wavelengths shown
Helium Emission Spectrum
Alkali and Alkaline Earth Metal Emission Spectrum
This emission spectrum is seen when a diffuse cloud of gas is made to glow. In stars, the bright surface of the star shines through a cooler layer that absorbs at the same places in the spectrum as it emits. See: Kirchhoff's Laws of Spectroscopy
Under many circumstances, this spectroscopic analysis can be used to determine the chemical makeup of any object that is emitting its own light.
Sunlight was seen to contain a very large number of details in its spectrum that was used to determine the chemical makeup of the sun. Here's a spectrum of a sunlike star:
Stellar Spectrum
That allowed us to see what different elements are in the sun but it didn't tell us what the correct proportions were.
Our knowledge of the correct proportionality is the result of the work of Cecilia Payne-Gaposchkin in her Ph.D. thesis. This work was called "undoubtedly the most brilliant Ph.D. thesis ever written in astronomy". The technique is based on a careful analysis of the relative strength of different lines due to each element which were analyzed using the then very new theory of Quantum Mechanics. The details are far beyond the scope of any treatment on this site
What she found was that the Sun is mostly hydrogen and helium. The rest of the elements are basically the same as those that make up the Earth and in the same relative proportions.