What is a comet?
Comets are also known as "dirty snowballs," small bodies of ice that are leftovers from the formation of the Solar System about 4.6 billion years ago. The comets we see in our solar system begin as chunks of rock and ice. Short-period comets come from the Kuiper belt, and long-periods are believed to have originated in the Oort cloud which is farther out. When the gravity from a large passing body becomes strong enough, some large chunks of ice get pulled away from their orbits and redirected toward the Sun. When it gets close enough to the Sun, the heat melts some of the ice and evaporates it. Its characteristic gaseous tail is made when the melted ice extends away from the source of heat, being pushed out by the Sun's solar wind.
The comet is kept in motion on its trajectory due to gravity from all the planets and asteroids it passes. In the case of our own solar system, most of the gravity affecting it is from the Sun. Furthermore, the strength of the Sun's gravitational force increases as the comet moves closer, causing the comet to accelerate and further extend its tail, as it also experiences greater evaporation from the higher solar radiation.