The tail of a comet is caused by the sun's solar radiation pressure and solar wind. These force dust particles and ions to flow out from the comet, away from the sun. As a comet gets closer to the sun, the radiation pressure and wind get stronger, and the tail grows longer. The tail always extends away from the sun, so as the comet moves away from the sun, the tail is ahead of the comet's general direction of movement (although not directly ahead, just farther away from the sun than the comet's head).
Today's brain teaser courtesy of Braingle.com.