Galaxies are formed by billions of stars, dust, and other materials, bounded between them by gravity. These massive stellar bodies are present in many shapes and sizes. However, the first galaxies were formed, all of them, in the same way, and it has been their following evolution that has determined their current configuration. Nowadays, galaxy formation is still going on.
The primitive universe
Short after the Big Bang, the universe was composed of hydrogen and helium, and trace amounts of other elements could be found, like lithium. In this sea of elements there were denser zones in material than others. As expected, the densest zones, that to say, with more material, attracted the surrounding material. These zones made themselves even denser, for which they attracted more material, creating a never-ending cycle. This provoked the formation of enormous clouds of gas, with an increasing density. And so, the first protogalaxies appeared.
Inside these protogalaxies places where stars are born, named nebulae, were formed. In these regions were born the first stars. These stars lived short and violent lives, and seeded the following star generations with the materials created at the moment where they exploded in the form of supernovae.
A galactic dance
Despite what I have explained before, this isn’t the only existing concept of a galaxy formation. In fact, many times we talk about galaxy formation when two or more galaxies “collide” forming a much bigger galaxy, with more stars and a much more massive black hole at the centre.
This is what occurred to the first galaxies. The latter were bounded gravitationally and merged the ones with the others, to subsequently form the great spiral galaxies that we know nowadays.
In fact, this process of galaxy formation still goes on in the present day. For example, our galaxy (the milky Way) is expected to collide against Andromeda (one of the closest galaxies to us), in about 4 billion years, to create an even bigger elliptical galaxy. We can see examples of these enormous galaxies throughout the universe.