Our Solar system formed around 4.5 Billion years ago from a cloud of dust and gas. This cloud had a small rotation as every object in space has some small amount of rotation. When this cloud collapsed due to gravitational event of maybe a nearby dying star, the temperature became hot enough to support nuclear Fusion and the Sun was formed.
The Sun that was formed was 10 to 20 times more Luminous and radiant than it is today, also called a proto-sun. This proto-sun was radiant enough to drive the remaining gas away from the inner solar system, leaving behind the rocky cores, also know as the inner solar system or terrestrial planets.
The Sun was not capable to give off enough radiation to drive gas from the outer solar system, thus those rocky cores with the passage of time were able to accumulate mantles of gas making them gaseous planets. The outer planets also called the gas giants are almost made of hydrogen and helium.