When comet comes near to the sun due to large heat some of the surface ice vaporizes and fly away from the sun to a large distance forming the tail so its length increases while coming towards sun. Whereas going away from the sun, temperature goes on decreasing and rate of evaporation also decreases which causes to decrease its tail.