A possible answer for this is that, light emitted from the galaxies travelled a billion miles all the way to earth, where the hubble space telescope picked up this light through its sensors, and was able to construct an image of the galaxy

but if this is true, and galaxies are billions of miles away, shouldn't the light particles emitted from the galaxies be scattered all over the place? after all they have been travelling from millions of years, and have probably collided with asteroids and other foreign objects. What were the chances that about 95% of the photons actually reached earth, giving us a very detailed image. 

Consider the andromeda galaxy which has a distance of 1.492 × 10^19 mi from earth. If light emitted from the galaxy travels in all directions, then how is it that we can still map out the entire galaxy, evident from the photo below?

Shouldn't like half of the galaxy be missing since photons could have hit other objects, and "never have reached earth"? 



There are two reasons that often — but not always — light from galaxies millions and even billions of lightyears away make it through the Universe and down to us:

Particle number and particle size


First, the intergalactic medium (IGM) is extremely dilute. The number density of particles out there is of the order $n\sim10^{-7}\,\mathrm{cm}^{-3}$, or roughly 26 orders of magnitude lower that the air at sea level! That means that if you consider a tube from Andromeda to the Milky Way with cross-sectional area of $1\,\mathrm{cm}^{2}$, it will contain roughly one microgram of matter (thanks to Rob Jeffries for catching a factor $10^6$ error).
Second, even if a photon comes close to an atom, it will only be absorbed if its energy matches closely some transition in the atom. Since most of the atoms are ionized (and thus should be called plasma instead, but in astronomy the distinction if often not made), there are no electrons to absorb the photon. The photons are more likely to interact with the free electrons via Thomson scattering, but the Thomson cross section is immensely small $(\sim10^{-24}\,\mathrm{cm}^{2})$, so even if you consider the CMB photons — which have traveled through the Universe almost since the Big Bang — only around 5% of them have interacted with electrons on their way.


In other words: The amount of transmitted light depends on two factors: 1) The amount of matter along the line of sight, and 2) that matter's ability to absorb the light. In the IGM, both are tremendously small. When the light enters the interstellar medium (ISM) inside our galaxy, it may encounter denser clouds with atoms that are able to absorb the light. But usually (although not always) "dense" is still very dilute compared to Earth's atmosphere.

Mathematical expression

In general, if a beam of light traverses a region of particles, each with a cross section $\sigma$ (measured e.g. in cm$^2$), passing $N$ particles per area of the beam (measured e.g. in cm$^{-2}$), then the opacity of the medium is given by the optical depth $\tau$, defined by
$$
\tau \equiv N \, \sigma.
$$
The transmitted fraction $f$ of photons is then
$$
f = e^{-\tau}.
$$
In general $\sigma$ depends on the wavelength, and thus part of the spectrum may pass unhindered, while another part may be completely absorbed.

The figure below (from here) shows the spectrum of a quasar lying at a distance of 22 billion lightyears, i.e. $10\,000$ times farther away than Andromeda. You see that there are several thin absorption lines (caused by intervening hydrogen clouds whose densities are a factor of 10-100 higher than the IGM), but still most of the light makes it down to us.



Because the light we see from this quasar was emitted so long ago, the Universe was considerably smaller at that time, and thus the density was larger. Nonetheless, only a small fraction is absorbed. The farther away the light is emitted, the longer ago it was, which means smaller Universe, and higher density, and thus the more light is absorbed. If you consider this quasar (from here) which lies 27 billion lightyears away, you see that much more light is absorbed in part of the spectrum. Still, however, much light make it through to us.



The reason that it is only the short wavelengths that are absorbed is quite interesting — but that's another story.
