4.6 billion years ago with a wispy cloud of stellar dust which is part of a bigger cloud called the Nebula.
An exploding star, called the supernova, sends shockwaves causing clouds to collapse. The result: a flat spinning disk of gas and dust.
Gravity in the nebula pulled in the matter. The gravity was so strong that it combined hydrogen atoms into helium, releasing energy, creating our Sun. It gobbled up 99.8% of all the materials.
The leftover materials clumped together and became planet, dwarf planets, asteroids, comets and moons.
Rocky planets like Earth formed near the Sun while icy and gaseous materials collected further away creating gas and ice giants because it couldn't survive with all that heat.
The solar system as we know it today was formed. There are still leftover remains, comets and asteroids that haven't changed much at all since the formation of the solar system.