They use a technique called spectroscopy to stretch out the spectrum of light coming from a star. Dark lines in this spectrum tell you exactly what it’s made of.
You can see which stars have high amounts of metals, or which are mostly hydrogen and helium, leftover from the Big Bang.