This is a very broad question, so below I mention just the very key points.
According to the prevailing cosmological model for the early development of the Universe, known as the Big Bang theory, the Universe begun at about 14 billion years from a singularity that can be thought of a state of an infinite density and temperature, and since its creation the Universe began to expand and cool.
Right after the Big Bang the Universe was immensely hot, the energy density was incredibly high and no particles existed, but the Universe was extremely rapidly expanding and cooling, so that soon enough energy was allowed to be converted into various elementary particles: quarks, leptons, bosons.
It is generally assumed that initially there was an equal numbers of particles and corresponding antiparticles, but at some point an unknown physical process referred to as baryogenesis lead to a very small excess of quarks and leptons over antiquarks and antileptons.
As the Universe continued to decrease in density and fall in temperature quarks and gluons combined to form baryons such as protons and neutrons, and the small excess of quarks over antiquarks led to a small excess of baryons over antibaryons, which in turn after mass annihilation resulted in the predominance of matter over antimatter in the Universe.
Big Bang nucleosynthesis
After annihilation it was just the matter of time for the Universe to cool down to a point where remaining protons, neutrons and electrons could combine to form atoms.
Already a few minutes after the Big Bang neutrons combined with protons to form the lightest nuclei (H-1, H-2 or D, H-3, He-3, He-4, Li-6, Li-7, Be-7, and Be-8) in a process called the Big Bang nucleosynthesis, and roughly 378,000 years after the Big Bang the Universe cooled to the point when the formation of light neutral atoms (mostly H and He with trace amounts of Li) became energetically favored.
Eventually, most of the protons in the Universe were bound up in these neutral lightest atoms and matter was uniformly distributed in the Universe.
But the gravitational attraction of the nearby matter by slightly denser regions and the subsequent growth of such regions resulted in formation of gas clouds, stars, galaxies, and other large structures.
The formation of stars meant that the extreme conditions (temperatures and pressures) for the nuclear fusion were recreated and light elements (H, He and Li) from which stars were formed started to turn into heavier one by a process referred to as the stellar nucleosynthesis. Stellar nucleosynthesis has its endpoint, namely, when nuclear fusion reaches nickel-56 which then undergoes radioactive decay into iron-56 the process stops. The reason is that iron-56 has one of the highest binding energies of all of the isotopes, and thus, no more energy can be extracted from an iron-56 nucleus, either by fusion or by fission. But it is not the end of the story.
When the core of a star converts to iron-56, the nuclear reactions stop. And that is a big problem, since so far the star balanced the forces of gravity pulling its materials inward by nuclear reactions pushing them outward, but now all the reactions pushing outward stop. What happens then depends on how massive the star is. Supergiants, that have masses from 8 to 12 times the Sun, gravitationally collapse in supernova explosions accompanied by a large burst of neutrons. These neutrons are captured by heavy nuclei, such as iron-56, and the resulting neutron-saturated isotopes then undergo beta decays producing stable isotopes of elements heavier than iron.