About 4.5 billion years ago, out solar system was
nothing but a cold dust cloud floating in outer
space. The dust came from even older suns (stars)
that had exploded after much of their fuel was
used up. (That's pretty amazing to think, eh?
All the matter that makes up our bodies was once
at the heart of an exploding star!). The force of
gravity caused the dust cloud to come together
into a tighter and tighter ball. It began to spin
and heat up and eventually formed the planets and
the Sun (our sun is much smaller than the older
suns that had exploded earlier). Learn more about
it at:planet
formation

Answer 2:

That is an open-ended question to which the best
answer I could give you is "nobody knows for
sure". The prevailing theories feature the sun
having a disc of spinning matter around it as it
collapsed to form a star, and that spinning matter
itself coalesced to form asteroids, comets, and
other bodies, which subsequently agglomerated to
become planets, and those planets with a mass
about ten times that of the Earth had enough
gravity to then trap an atmosphere massive enough
to swell them up to gas-giant sizes (the mass of
the atmosphere alone being sufficient to attract
more). There are a number of questions concerning
why the planets in our solar system have the
orbits that they do and the distribution of mass
that they do (i.e. rocky planets inside, gas
giants outside), as well as why all the planets
have orbits so close to circular. These questions
simply have not been answered definitively.