Astronomers measure the distance to a galaxy in the same way we estimate the distance to an oncoming car by the brightness of its headlights. We know from experience how much light a car's headlights emits, so we can determine how far away the car is.
To measure the distance to a galaxy, we try to find stars in that galaxy whose absolute light output we can measure. We can then determine how far away the galaxy is by observing the brightness of the stars. Such stars can help us measure the distance to galaxies 300 million light years away.
If a galaxy is too far away for us to distinguish individual stars, astronomers can use supernovae in the same manner, because the light output of supernovae at their peak brightness is a known fact. Supernovae can be used to measure the distance to galaxies as far as 10 billion light years away.
HubbleSite and STScI are not responsible for content found outside of hubblesite.org and stsci.edu