Why do galaxies form?

Not an astronomer by any stretch of the imagination.

I understand how solar systems could form, the gravity from the star keep most (all?) solar system matter in some kind of orbit around itself.

But what is keeping millions of solar systems bunched together?

Are there rogue solar systems that live outside of galaxies?

I’m not an astrophysicist by any means, but the basic concept is, the same gravitational forces that keep solar systems together also keep galaxies together, and galactic clusters together.

Check out the wikipedia page on intergalactic stars. Intergalactic star - Wikipedia You’ll get a starting discussion on why stars ore found in galaxies, and how they can end up ejected.