This is a long type of question but I’ll try to get it in as short as possible.
A principle that must be understood is that matter comes from energy. Einstein’s famous equation, E=mc^2, relates the mass of an object to the amount of energy it “contains”, which, even for relatively small objects, is a lot. Every person, planet, star, and galaxy across the universe is comprised of matter and, subsequently, energy.
When the universe was very young, it was condensed into a much, much smaller and much, much higher energy form. At its earliest point, the universe had all of its energy condensed into a very small space that was unbelievably hot. This high-energy state prevented matter to exist, so all of the energy we can imagine was compacted here! It’s pretty mind-boggling!
When the universe began to expand, the energy that was making the universe hot began to spread and become less concentrated. This took a really long time for this to occur, on the order of hundreds of thousands of years. Once this high-energy became spread out and stable enough, matter could essentially “condense” from the energy. Too high energy = no matter, low enough energy = matter.
More complex matter requires more stability, so when matter started to form, it still took a lot more time for it to reach today’s point of diversity. All of the elements on the periodic table didn’t just suddenly form, first most of the components of the standard model of quantum physics took form followed by protons, neutrons, and electrons, and, finally, hydrogen, the simplest element, formed.