There are several different methods.
The first, and most simple, is the parallax method. This is akin to holding one finger out at arm's length and then closing one eye and using it to judge the position of something far away. Then close the other eye. You'll notice the position relative to your finger will shift. Using a bit of geometry, you can calculate how far the object must be. Only for this to work, we use the base line as the distance of the earth 6 months across (so 2 AU).
Another method is to look at the brightness of the star. From its spectrum, we can learn how bright a star SHOULD be by figuring out where it should be on an Hertzsprung-Russel diagram. Then we can look at how bright the star IS. Since brightness falls off as 1/d^2, distance can be calculated fairly simply. However, this assumes that there's nothing like clouds of dust making the star look dimmer.
Once we get out of our own galaxy, distances get harder to measure. Parallax fails all together, and for all but the nearest galaxies, the stars seems to all run together and it becomes somewhat difficult to find a single star from which to get a spectrum since most often, other stars will get their spectra in too. Thus new methods must be devised.
The first, originating from the early 1900's uses a certain type of variable star known as a Cephid variable. These stars have very regular periods, and the shorter the period, the brighter the star. This allows us to know again, how bright the star should be. This is the method Edwin Hubble first used to prove that the Andromeda Nebula was indeed an "island universe" (galaxy) and now some small nebula.
The next method is using the doppler shift. The doppler shift is a change in frequency caused by an object moving relative to you. We've all experineced this if you've listened to a police car pass you with its siren on. As it's approaching, it has a higher pitch. Once it's moving away, its pitch goes down. The same thing happens with light. When an object moves towards us, its light gets its waves squished together and makes the light "blue shifted". When it moves away, it becomes "red shifted". It's been discovered that the further a galaxy is, the faster its moving away from us. Thus, knowing this, you can determine how far a galaxy is by looking at how fast it's moving away by looking at the amount of red shift. It should be noted that not ALL galaxies are moving away from us. Ones within our local group are sometimes moving towards us because a gravitational attraction overrides the otherwise moving away (which is caused by the Big Bang).
For the VERY far away galaxies, we use still other methods. Another common one is to look at supernovae and again figure out how bright they should be compared to how bright they are. Based on the elements present in the spectrum of a supernova, it can be determined what type it should be, which gives us absolute magnitude. While this method is used quite frequently, it's not really reliable since it ASSUMES we know exactly how supernovae work. While almost every other method I've mentioned (with the exception of parallax) makes a similar assumption on knowing how something works, supernovae are not as well understood as the other objects we rely on. However, since supernovae can be seen clear across the universe, this method is one that is used quite frequently.
Those ones are the most common, but there are still other, more obscure and unreliable ones out there. One example is the Tully-Fischer relation which describes how bright a galaxy of a certain type should be and again, we can figure out how bright it should be. But brightness of a galaxy is affected by so many variables that this method is not reliable as well.
I hope that helped. Let me know if you have any questions.