If we have small wrinkles or hills and valleys early on in the universe, matter will tend to fall into the valleys, eventually producing dense regions that become the sites of galaxies.
In the discussion that follows, I will often represent these wrinkles by a sort of "top view" where the color coding refers to the density of matter (dark regions have more matter, light regions less).
Needless to say, this is a bit of an idealization for illustative purposes. Cosmologists actually run computer simulations to track how matter collects into valleys. For example, here is a simulation running forward in time which shows how particles collect and enhance small initially small wrinkles
(simulation and movie courtesy of Andrey Kravtsov)