A comet's tail always points away from the sun because solar wind acts on the nucleus of the comet, and the impacted materials follow the direction of the energy. The tail actually consists of carbon dioxide, or dry ice, changing directly to gaseous form, or melting water, with organic molecules.
As the comet travels, the solar energy elevates the state of matter of the nucleus. Previously frozen carbon dioxide sublimates as a result, while liquid water turns to vapor. In the process, the comet releases other organic molecules that the crystals of ice and dry ice had previously held in place.