Best models to date agree that hydrogen and helium filled all of space more or less evenly when the universe was very young, but that some areas were very slightly more dense than others. The models then just use standard laws of physics to explain what happens next. Initially everything expands together but the gravity of the slight density enhancement will slow the expansion in that area. Eventually expansion will be halted in the denser regions and the material starts contracting due to gravity. We now have protogalactic clouds which will in the long run become galaxies.
Stars start forming, but the most massive ones became supernovae within a few million years.
Shock waves slowed the contraction of the galaxy, heated up the gas and slowed next wave of star formation. Some evidence that what we thought were the oldest stars in our galaxy are actually 2 nd generation stars, making the Sun a 3 rd generation star. The oldest stars have heavier elements in them besides hydrogen and helium, which had to have come from a previous episode of stellar formation and death. Also older stars as well. See old red stars in elliptical galaxies and in the spheroidal part of the spiral galaxies.