The first part of the answer seems to require dark matter, which, being impervious to the smoothing effects of photons and electrical interactions, had already gotten rather more concentrated. It's clumps attracted the now neutral baryonic matter.
However, just as expansion of the universe had cooled the overall cosmos, local contraction (to form galaxies and stars) heats it back up, and that heat generates pressure which resists the contraction. Consequently, galaxy and star formation require cooling. So how do you cool a cloud of hydrogen and helium gas?
When you turn off the burner on an electric stove, it cools by conduction and radiation of infrared radiation. In a vacuum, infrared radiation does all the cooling. A cloud of hot hydrogen and helium has no cool reservoir to conduct to and it can't radiate unless the atoms can be excited (by mutual collisions) up to the lowest excited hydrogen level, the Lyman alpha level. So how hot does it have to be to get to that lowest level? The temperature corresponding to the Lyman alpha frequency is 79,000 K, but because hot atoms have a Maxwellian energy distribution, a fair number of collisions at sufficient energy will take place at 20,000 K or so, but it gets hard to cool pure hydrogen and helium much below that.
Once stars do manage to form and die, they spew metals (again, in astro speak, anything other than hydrogen and helium) and cooling and star formation become much easier to achieve.