Planetary motion obeys Kepler’s laws. A comet has an eccentric elliptical orbit around the sun. The sun lies at a focus of the ellipse and the area swept out by a line joining the centre of the sun to the centre of the comet remains constant throughout the orbit. That is, equal areas are swept out in equal time. For this to happen the speed of the comet must vary. When near the sun it travels further along its orbit so that the same area area is swept out as when it is further from the sun and travels less distance along its otbit. A greater distance in the same amount of time means a higher speed.
The orbital speed is derived from the angular momentum, mvr, which has to be conserved. Kepler’s laws follow from the principle of conservation of angular momentum. Since the mass, m, is approximately constant (although particles from the comet are lost in space, the comet has a substantial core of material vastly greater than the mass of material in its tail), the quantity vr (velocity times radius) is constant, and r is the distance between the comet and the sun, so v must increase when r decreases.