There are two methods, one more reliable than the other (though both are pretty good.)
Key point: The brighter a star is, the more detail we can see in its spectrum -- you can think of it as being able to magnify the spectrum more so as to be able to see finer details. This also allows us to see fainter lines (not all spectral lines are equally intense.)
We have very extensive reference spectra of all the elements under a wide variety of conditions, so when a star is bright enough we see many spectral lines and can match them up to reference spectra. No two elements have similar spectra, so if you can see many lines, you can't easily confuse one for another.
When you can account for nearly all of the lines in a star's spectrum by a plausible set of elements in plausible abundances and all with the same red shift, you have a very, very reliable match and accurately know the object's red shift.
But distant stars (and galaxies) are very faint and so the spectra we get are very low resolution and show only a few of the strongest spectral lines. They can frequently be matched because hydrogen is so common that the strongest lines seen will be hydrogen lines and even if you can only spot two or three of them, if they bear the same relative positions as bright hydrogen lines, you can safely identify them and read off the red shift. This is less reliable, but still reliable enough that it rarely is a problem.
In the very dimmest cases you may only see a single line. You have a very rough idea of the redshift from the object's brightness and all you can do is assume it's the strongest hydrogen line and see if it's in roughly the right position given the distance deduced from the object's brightness and type. This works too, but is still less reliable. (Much of Hubble's original work relied on just a single line for the more distance objects, but proved to be correct when observed later with dramatically better equipment.)