From the perspective of an Earth-based observer, the planets constantly appear to change positions in the sky -- a fact reflected in the word "planet" itself, which comes from the ancient Greek for “wanderer.” These apparent motions can be explained by assuming that the planets move on near-circular orbits around the sun. The dimensions of these orbits have remained constant throughout human history, but on much longer timescales they have changed due to planetary migration.
The motions of the planets are governed by the forces acting on them. The largest of these forces is the sun's gravity, which keeps the planets in their orbits. If no other forces were involved, then orbits would never change. In reality, though, there are several other forces involved, called perturbations. These are smaller in magnitude than the sun’s gravity, but large enough to cause planets to change their positions over long periods of time. Perturbations include the gravitational influence of large planets such as Jupiter and Saturn, plus the cumulative effect of collisions and close encounters with asteroids and comets.
Early Solar System
When the planets first formed, about 4.6 billion years ago, the solar system was still filled with large quantities of gas and dust -- enough to exert a significant gravitational pull on the newly formed planets. The gas and dust was concentrated in a dense, rotating disk, and this became the main driver of planetary migration in the early history of the solar system. One effect of the disk was to pull the smaller rocky planets -- Mercury, Venus, Earth and Mars -- inward toward the sun.
The Outer Planets
Jupiter, the largest of the planets, was initially pulled inward as well. It stopped when it was about the same distance from the Sun as Mars is today, probably held back by the gravitational influence of Saturn, the next planet outwards. Jupiter and Saturn then drifted outward again, approaching the orbits of the outermost planets, Uranus and Neptune, which were closer to the sun than they are today. By this point, most of the interplanetary gas and dust had dissipated, and the pace of planetary migration slowed down for a time.
A Stable Configuration
Around 3.8 billion years ago, not long before the first primitive life appeared on Earth, there was a dramatic second phase of planetary migration. This was triggered when the orbits of Jupiter and Saturn became briefly locked together, with Saturn taking exactly twice as long as Jupiter to complete a circuit around the sun. This proved to have a destabilizing effect, not just on Jupiter and Saturn, but on Uranus and Neptune as well. To compensate for this instability, the positions of all four planets changed rapidly. Jupiter migrated inward, while Saturn, Uranus and Neptune migrated outward. After just a few million years -- a brief period in astronomical terms -- the planets had settled into stable positions very close to those we see today.