Comets are small bodies originating in the furthest reaches of the solar system, which orbit the sun in a highly elliptic path, thus obeying the laws of universal gravitation.
Comets are a mixture of ice and meteoric matter. Near the sun, their volatile elements sublimate to form a cloud ending in a long tail which is always directed away from the sun.
The path of Halley’s comet, with its 76 year period, is simulated here. You can click on the comet to drag it to different initial conditions, and so see other possible trajectories of such a body in a gravitational field.