The legend found its origins in incident wherein two notorious demons Shambhu and Nishambhu started creating havoc in God Indra’s kingdom i.e. heaven. When gods started losing the battle to demons they went to hide in Himalayas the holy mountains also the home of Lord Shiva and Goddess Parvati. They summoned the help of Mahamaya Durga. Goddess of Kali who was born from Goddes Durga’s forehead as Kal Bho Nashai. Goddess Kali was created along with her two escorts i.e. Dakini and Jagini with the sole purpose to save heaven from the clutches of demons.
Eventually goddess Kali killed these demons and while this killing rage she lost control over herself and became a havoc for everyone else. That is why Lord Shiva took her by surprise by lying down under her feet.
People perform kali Puja to kill their negative emotions and ego within and around them. It is also performed for the general happiness, health wealth and prosperity of people.
It is basically a tantric puja which is held at midnight on Amavasya i.e. the new moon night in the month of November. Incidentally Diwali the festival of lights also falls in the month of late October or November every year. So people get double reason to celebrate the Amavasya period- one for Kali Puja and second for Diwali!