Most of the universe is pretty empty in terms of the density you're used to in daily life. It's perhaps not that stars and galaxies are far apart, but that they are pretty compact.
This is because baryonic matter (as opposed to dark matter) can lose energy via electromagnetic radiation and hence condense to smaller and denser objects. This is only opposed by angular momentum (which cannot be simply radiated away) forcing disc-like structures such as the Galaxy and proto-stellar and -planetary discs.
One of the obvious answers is gravity, Galaxies are gravitationally bound systems of stars, interstellar gas and dark matter, often hosting a central supermassive black hole. So anything in its hill sphere will fall into the galaxy.
We also know that the universe is expanding which explains the large distance and emptiness because the objects are moving in relation to one another. Neither is moving through space, but space is expanding.
So two galaxies that used to be 1 billion light years apart are now 2 billion light years apart. The expansion of the universe is the formation of new space between the Galaxies.