I suspect you might be slightly confused by light year being a unit of distance. You are basically asking how they know how far the light source is (same thing as how long it took the light to come but less clumsy).
There are many methods for things at different distances. These are collectively known as the [“cosmic distance ladder”](https://en.wikipedia.org/wiki/Cosmic_distance_ladder). For nearer objects we can use parallax caused by the movement of the Earth orbiting around the Sun. However, for further distances, we may use “standard candles”, one example being Cepheid variables which are stars whose brightness changes regularly and for which there is a known relationship between their luminosity and their period. By seeing how bright they are and checking that against how quickly their brightness changes, we can estimate the true distance of the galaxy they are in.
However, none of these methods are very precise and reliable.
Latest Answers