Generally, comets got kicked out from their home which are the Oort Cloud and the Kuiper Belt. This phenomena occur due to the pull of the gravity by planets or stars. Then, their journey of growing tails begin by moving toward the inner solar system.
Far from the Sun, small comets look the same as small asteroids, completely frozen and this dirty snowball is in solid form. As it approaches the Sun, it starts to heat up and ices begin to vaporize into gas that easily escapes the comet’s weak gravity. A combination of solar radiation pressure and the solar wind (stream of charged particles from the Sun) sweep the vaporize materials and dust back forming two separate tails :
An ion tail forms when UV from the Sun rips electrons from gas atoms in the coma (cometary), making them into ions through ionization. Then, the solar wind carries these ions away from the Sun resulting straighter and narrower tail.
On the other hand, a dust tail contains small particles (size similar with cigarette smoke). The reason this tail forms due to the presence of the solar radiation pressure that pushes on these particles, shoving them away from the nucleus of the comets. Due to the relatively weak pressure from the sunlight , the dust particles end up forming a diffuse and curved tail that typically appears white or pink relative from the Earth
Apparently, based on recent observations, comets are not the only objects in our solar system that grow tails. These observations stated that asteroids can also sprout dust tails on occasion.