Diagram of the Oort Cloud and Kuiper Belt. Click here for original source URL.
The story of comets starts 4.6 billion years ago with the large cloud of gas and dust that was destined to form the Sun and the Solar System. Comets formed by aggregations of ice crystals and carbon-rich dust grains in the cold, outer regions, where the giant planets were also forming. Most of the material outside of the Sun collapsed into a plane; that’s why we see all the planets in a thin strip of the sky near the ecliptic. Comet nuclei that formed in the region beyond Pluto remained in a similarly flattened distribution, forming the Kuiper belt. However, many of the comet nuclei came too close to one of the giant planets, and the planets’ strong gravity flung them into highly elliptical orbits that took them hundreds or thousands of A.U. from the Sun. Since they could be flung in any direction, they ended up in a spherical distribution. This is the Oort Cloud.
Nucleus of Halley's comet. Click here for original source URL.
Comets have remained "stored" in the Oort Cloud and Kuiper Belt during much of the solar system’s history. However, as a result of random gravitational disturbances, perhaps from passing stars or the giant planets, comets in the Oort Cloud or Kuiper belt occasionally find themselves on trajectories back towards the inner or outer solar system. The set of comets that come close to the outer planets are called Centaurs, after the half-man, half-horse creatures of Greek mythology. Once on an elliptical orbit among the outer planets, a Centaur may wander for a million years or more before being kicked into the inner Solar System by a close encounter with a giant planet. As they near the inner Solar System, the Sun warms the comets. Their ice sublimes, and they temporarily form the familiar heads and tails that give comets their traditional appearance.
Halley's comet in 1986. Click here for original source URL.
Some of the comets that make it into the inner Solar System have additional close encounters with planets, which change their orbits further. In a few cases, the gravitational tug of a planet greatly reduces the size of the elliptical orbit. This process created comets that have repeatedly appeared in our skies in recorded history, such as Halley’s Comet, with its 75-year period, or Comet Encke, with its 3-year period. At aphelion, Halley’s Comet makes it back to its original "home" in the Kuiper belt, with a maximum distance from the Sun of 36 AU. But the orbit of Comet Encke has been modified so much that now it never gets farther than 4.1 AU from the Sun.
The life story of a comet is a complex gravitational ballet. For most comets, the excitement comes early on, when they are flung into the Oort Cloud by a close encounter with a giant planet. They travel in lazy, looping orbits of the Sun, with periods of hundreds of thousands or millions of years. Most of their lives are spent in the deep freeze of space, so far from the Sun that it would appear as a mere point of light, not too different from the other stars in the sky. Occasionally, they have an encounter with a giant planet that sends them into the inner Solar System. Only then does the Sun’s heat bring the frozen rock to life, liberating gas and dust into a luminous coma and tail.
The behavior of comets is very different then the familiar motions of the planets. Normally, the orbit of any body in space can be predicted far into the future, using the laws of Kepler and Newton. These motions are very orderly. We are accustomed to the idea that the orbits of the Earth and other planets do not change much over time. However, when small interplanetary bodies drift very close to planets, their orbits will change radically. For example, if a Kuiper belt body passed near the top of Neptune’s atmosphere, it might be flung onto an orbit that would lead to a close approach to Saturn. That encounter might fling it into the inner Solar System, where it could hit the Earth millions of years later. Yet if it approached Neptune on a path altered by only a few kilometers, it might completely miss Saturn and never come into the inner solar system at all. In other words, a tiny change in initial conditions could lead to two radically different histories. Jets created during this heating can also effect the comets' orbits, pushing them onto new and unpredictable paths.
Orbits that can change drastically with only a tiny change in initial conditions are called chaotic orbits, because they are almost impossible to predict. Chaotic orbits are examples of a wide range of physical phenomena that are not easily predictable - the weather is a familiar example. Most interplanetary bodies are not on chaotic orbits, but the existence of even a few bodies on chaotic orbits makes the future fascinatingly uncertain. There could be bodies now near Saturn or Uranus, for which a change in path by only 100 meters might determine whether Earth has a catastrophic impact a million years from now!