A comet is an icy small Solar System body that, when passing close to the Sun, heats up and begins to outgas, displaying a visible atmosphere or coma, and sometimes also a tail.
As the comet approaches the sun, the heat evaporates the ice in the comet turns straight into water vapor. As everything else evaporates, the water vapor goes straight back behind the comet and into the trail.
They go around the sun in a highly elliptical orbit. Like all orbiting bodies, comets follow Kepler's Laws - the closer they are to the Sun, the faster they move
They only have tails by the sun because when they are far from the sun, comets are extremely dark and cold. And as they come closer to the sun they start to warm up, and the debris blow away and form a tail behind the comet.