A comet is a collection of dust, rocks, ice, and frozen gases which is between half a mile and 100 miles in diameter. A comet's tail does not exist for nearly all of the comet's orbit. It won't appear at all until the comet's orbit takes it close to the sun, and it will disappear again after the comet's orbit takes it away from the sun.
Why a comet's tail depends on its orbit
A comet's orbit is a long, narrow ellipse. The near side of the ellipse is very close to the sun. Halley's Comet goes inside the orbit of Venus. Several short-term comets have points of closest approach to the sun, or perihelions, inside the orbit of Mercury. The closest sungrazing comets may come within a few thousand miles of the sun's surface.
At its far end, or aphelion, all comets go at least as far as the orbit of Neptune. Most short-term comets have their aphelions between Neptune and Pluto. Most long-term comets come all the way from the Oort Cloud, which reaches more than 1 light-year away from the sun.
The only time a comet is visible without an extremely powerful telescope is when it comes close enough to the sun for part of the comet to evaporate and create a visible coma and tail. This usually happens around the same distance as the Earth is from the sun.
This is not a coincidence. Earth lies in the habitable zone where liquid and gaseous water can be stable. A comet cannot produce a tail until its frozen water starts melting and then evaporating.
The tail will last until the comet has swung around the sun and starts heading back out to the outer reaches of the solar system. At most, this is only a few days or weeks out of an orbit which may last millions of years.
Composition of the comet's coma and tail
A comet is a dirty snowball made mostly of water-ice and rock. To be classified as a comet by NASA, a comet must be composed of at least 85% ice, which can include ice consisting of carbon monoxide, carbon dioxide, methane, and ammonia. It may also include complex organic molecules such as amino acids, the basic building blocks of life.
When the comet is close enough to the sun, the comet's coma will be created from volatile materials which are sublimating or evaporating into gases and plasma, as well as from any freed dust. A nucleus which is only about 30 miles across can produce a coma which is larger than Jupiter.
The tail will be made of the same substances as the coma. The coma must appear before the comet can produce a tail.
How the comet's coma becomes the tail
As the solar wind interacts with the ionized gas of the coma, it blows part of the coma away from the sun. This becomes the comet's tail. The interaction between the solar wind and the ions in the comet's tail causes the comet tail to point away from the sun.
The comet's tail can be be up to 100 million miles long. It is always much larger than the comet. However, the tails of extremely rocky comets may be only a few times larger than the comet.
The coma and tail of the comet are much brighter than the frozen comet was. Some of the brightness is because of reflected light. Some of it is produced directly by the comet's ionized gases. The interaction between the solar wind and the gaseous ions also generates X-rays.
Many comets also form a second, much fainter tail, which is made up of non-ionized dust from the coma. This tail points back along the direction of the comet's orbit. When the comet is leaving the sun, this dust trail may point back towards the sun. This can create a strange situation where the primary comet tail is pointing away from the sun as normal, while a secondary comet tail is pointing in the opposite direction.
After the comet leaves the vicinity of Earth and Mars orbits, its tail begins to dissipate as the coma freezes. When the comet is completely frozen, its tail vanishes, not to be seen again until the comet's long orbit once again takes it close to the sun.