From the onset of the cosmos, energy expanded and dissipated. Once matter formed, the early universe was a hot, dense plasma of emerging photons, electrons, and protons.
Depending upon the account you cotton to, it took anywhere from 10 seconds to 10’s of billions of years for the cosmos to cool enough for atoms to form: protons captivating electrons via the music of emerging electromagnetism.
Neutrons weigh 1.00137841917 that of protons; exactly the ratio needed for nucleosynthesis: the creation of atomic nuclei in stars. Further, the electrical charge of electrons neatly balances that of protons. Without these precise balances, there would be no matter in the universe.
At 3,000 Kelvin (K), electrons slowed enough to be snared by the gravitational force of atomic nuclei and set up housekeeping as atoms. Only the lightest elements – hydrogen and helium – spontaneously arose as primordial gases.
The universe was still mostly dark, though scattered with matter, and seething with energy. The slightest variations in gravitational densities acted as seeds for the distribution of what would become stars and galaxies.
Reionization is one of the major milestones in the universe’s history. ~ American astronomer Brant Robertson
A peculiar transition may have happened 13.6–12.8 BYA: reionization. Something stripped the electrons off atoms.
Radiation bursts from star formation in the 1st generations of galaxies may have caused reionization, though how it came about remains mysterious. Whether reionization even occurred is less than certain. The cosmic particle soup had thinned enough prior to reionization that photons could travel freely, turning most the universe’s matter into the glowing ionized plasma that abides to this day.
◊ ◊ ◊
From the appearance of primordial gas clouds it took hundreds of million years for the nascent stars to accrete. The 1st stars brought light and warmth into the cosmos, as well as melting hydrogen and helium together, forging the heavier elements, including carbon, nitrogen, and oxygen. After a few hundred million years of stars making matter, all the natural elements had emerged.
As they were the first to sup on primordial matter, the earliest stars were monstrously large. Some were the size and luminosity of 100 million suns.
The distortion of gravity was slow to make its cosmic presence felt; only with the advent of the first stars did gravity emerge in any significant way. As gravity came into play, swirls of matter coalesced into nebulas, forming galaxies ~13.2 billion years ago.
Gravity is not the only force binding cosmic matter. Magnetism was instrumental in shaping accretion disks that became stars and black holes, and in its flux begetting glue to nascent galaxies.
Metals – such as life-essential iron – are rather evenly spread throughout the cosmos. This is the legacy of an energetic episode for matter creation, when exploding stars and black holes at the hearts of young galaxies were especially vigorous.