There are two theories as to how planets in the solar system were created. The first and most widely accepted, core accretion, works well with the formation of the terrestrial planets but has problems with giant planets such as Neptune. The second, the disk instability method, may account for the creation of giant planets.
The core accretion model
Approximately 4.6 billion years ago, the solar system was a cloud of dust and gas known as a solar nebula. Gravity collapsed the material in on itself as it began to spin, forming the sun in the center of the nebula.
With the rise of the sun, the remaining material began to clump together. Small particles drew together, bound by the force of gravity, into larger particles. The solar wind swept away lighter elements, such as hydrogen and helium, from the closer regions, leaving only heavy, rocky materials to create terrestrial worlds. But farther away, the solar winds had less impact on lighter elements, allowing them to coalesce and compose Neptune and other gas giants. In this way, asteroids, comets, planets, and moons were created.
Although the terrestrial planets cycled through their original atmosphere, Neptune and other gas giants still have much of the same atmosphere they were formed with billions of years ago.
In order for this model to work for giant planets such as Neptune, it would require a third element of migration. Some scientists propose that Neptune originally formed closer to the sun, then traveled outward to its present position once the gaseous disk dissipated.
The disk instability model
The need for a rapid formation for the giant gas planets is one of the problems of core accretion. According to models, the process takes several million years, longer than the light gases were available in the early solar system. At the same time, the core accretion model faces a migration issue, as the baby planets are likely to spiral into the sun in a short amount of time.
According to a relatively new theory, disk instability, clumps of dust and gas are bound together early in the life of the solar system. Over time, these clumps slowly compact into a giant planet. These planets can form faster than their core accretion rivals, sometimes in as little as a thousand years, allowing them to trap the rapidly-vanishing lighter gases. They also quickly reach an orbit-stabilizing mass that keeps them from death-marching into the sun.
— Nola Taylor Redd, SPACE.com Contributor