The gods and demons churn a sea of milk using Mandara hill as the churning post, and Vasuki, the snake king, as a rope wound around the hill. This task was undertaken to bring up amrit - the elixir of immortality.
A lot of things were churned up before the amrit was obtained. One of the most notable item was a deadly poison. The moment the poison was brought up, it started poisoning and killing the whole creation. Everybody panicked and rushed to Shiv for help. Shiv drank the poison to save the others. The poison turned Shiv's throat blue. This is why Shiv is also known as Neelkunth - literaly: blue throat.
Now it was already decided among the gods that the Asuras or demons will not be allowed to drink the amrit since that would make them immortal and equal to gods.
But since they had helped in churning they could not be refused outright. So the demons were tricked in standing at the rear end of the queue with the gods in the front. The idea was that the amrit would be finished before the demons had a chance.
Among the demons were two Asuras, Rahu and Ketu, who caught on to the deceit. They disguised themselves as gods and jumped the queue.
As they were drinking the Amrit, the gods of sun and moon saw through their disguise and told Vishnu, who in turn severed the demons' head with his Sudarshan Chakra.
Since the nectar did not have the time to go below the throat, the bodies remained mortal (and subsequently perished) but the heads became immortal.
These two demons take their revenge on the Sun and the Moon once a year by devouring them (Solar & Lunar eclipse).
Astrologically, Rahu and Ketu, considered evil influences, are also capable of immense good since they have some amrit -the elixir of life- in them.