How can the composition of a star be determined?
Although mainly hydrogen, the composition of stars can vary
quite a bit from star to star. It depends on what materials were
around during the formation of the star that determines the
composition, which would have been dictated by what had been there
previously.
Large stars with high masses have relatively short life spans,
but towards the end of their life they enter into phases where
heavier elements are used as fuel, producing heavier elements
still. When the die, there is a supernova explosion. The heat and
pressure during this time is so immense that the heaviest elements
form through fusion.
The mixtures of elements left over from such events will spread
out and become part of new solar systems, when new stars beginning
to form. The different concentrations of elements will give us
stars of varying compositions.