Comets are one of the neatest phenomena we have the pleasure of observing within our own solar system. The most noticeable part of any comet is its tail. Capable of stretching thousands to millions of miles into space, the tail looks as if it traces out the path of the comet's travel, leaving a trail of debris where the comet once was, but this is not quite the case.
Our planet is wrapped in an atmosphere composed of nitrogen, oxygen, and a few other elements in small amounts. For an object in motion on Earth, the atmosphere creates a frictional force. For instance, when you stick your arm out of a moving car, you will quickly find yourself fighting the air to keep your hand in the same position as the friction attempts to force it backward.
Similarly, when meteors enter our atmosphere, a frictional force is generated that starts to burn off the outer layer of the rock. This transfer of energy creates a massive tail that stretches and fades in the direction opposite of the meteor's forward direction.
Comets are space objects made up of ice, rock and dust, and sometimes their orbits can bring them close to the Sun. As a comet approaches closer, two things happen. They start to heat up and they start to feel the solar wind, a consistent wind from the Sun that is blown out in all directions. This space wind is the result of high-energy particles escaping the star's gravity; they can travel at speeds of 671,000 mph all the way up to ~1,789,000 mph.
When the solar wind hits a comet, it creates a frictional force just like our atmosphere does on an arm sticking out the car window or a meteor streaking through the sky. The force of the solar wind on the comet sends the outer layers hurling out into space, and since this wind comes from the Sun, this means that comet tails do not trace out a comet's path, but rather stream out in the direction opposite of the Sun's solar wind.
Technically, a comet has 2 tails. One made of dust and the other made of gas ions. While both are a result of the solar wind, the dust tail leaves an arc shape in the sky whereas the gas tail will stretch out precisely in the direction of the solar wind. This can be seen in the images below along with other great comet photos.