Although the Universe is expanding so galaxies are in general quickly moving away from each other, on small scales gravity can still dominate. This is why
neighboring galaxies can collide,
galaxy clusters form, galaxies hold onto their stars, the Solar System stays together, and you stay on the Earth.