Here is my answer to a similar question posted on the physics stack exchange website.
Hubble's law (the law that deals with the expansion of the universe) applies to the expansion of space itself, i.e., if two objects stationary to each other that had no force between them were left alone the distance between would increase with time because space itself is expanding. This is what Hubble's law addresses.
In the case of the Milky Way and Andromeda galaxies (and all galaxies for that matter) there is a force between them: gravity. The gravitational force between the Milky Way and Andromeda galaxies has produced an acceleration that is causing the two galaxies to be moving towards each other faster than the space between them is expanding as calculated by Hubble's law. However, the vast majority of galaxies lie far enough away from the Milky Way that the gravitational force between us and them is small compared to the Hubble expansion and Hubble's law dominates.
In short, Hubble's law applies throughout the universe, but localized systems may have enough gravitational attraction between them that the gravitational effects dominate