This text was originally written to accompany a series of lectures that I gave at the `35th Saas-Fee advanced course' in Switzerland and at the Institute for Astronomy of the University of Hawaii. It reviews my current understanding of the dynamics of comets and of the origin and primordial sculpting of their reservoirs. It starts discussing the structure of the Kuiper belt and the current dynamics of Kuiper belt objects, including scattered disk objects. Then it discusses the dynamical evolution of Jupiter family comets from the trans-Neptunian region, and of long period comets from the Oort cloud. The formation of the Oort cloud is then reviewed, as well as the primordial sculpting of the Kuiper belt. Finally, these issues are revisited in the light of a new model of giant planets evolution that has been developed to explain the origin of the late heavy bombardment of the terrestrial planets.