There are two stories on the origin Kali Maa, and the one from the Durga Saptashati (a poem in praise of Durga Maa), which is part of the Markandeya Puran is more popular.
The Origin of Kali Maa
Long long ago there existed two powerful demons called Shumbhu and Nishumbhu. As they grew in strength, they usurped the vast empire of the King of Gods, Indra and dispossessed all the gods like Surya, Chandra, Yam, Varuna, Pawan and Agni. Both of them also managed to throw the god-host away from heaven. Sorely distressed the gods went to the mortal realm (Earth) and began to brood on how to get rid of these demons permanently. The solution was to pray to Durga Maa in her form of Parvati, the wife of Shiva. They reached the Himalayas and prayed to please the kind hearted Goddess Parvati. Agreeing to help, the body of Mother Parvati emerged a bright light in the form of a divine lady called Ambika. Her exit from Devi Parvati's body caused the latter to turn dark and black. She was then known as Kaushiki who began to dwell over the mountain ranges.
When the sycophants of the demons, Chand and Munda saw the dazzling light in the beautiful form of Ambika, they were enchanted by her superb beauty. They went to the demons Shumbhu and Nishumbhu and said, "Your Lordship! This woman is the most beautiful female in the entire Universe." They described her beauty in such superlative terms that Shumbhu and Nishumbhu could not resist sending their messenger Sugreeva to bring her to them.
Sugreeva reached Ambika and extolled the virtues of his masters Shumbhu and Nishumbhu to influence the Goddess. But she smiled indulgently and replied: "You may be right in the assessment of your masters but I cannot break my oath. I might have done it rather unconsciously but the fact is that now I stand committed to my oath, which is that whosoever can defeat me in battle and brow-beat me; whosoever can match my power, only he shall only be my master. So go and tell your masters to show their strength and win me in the battle."
The messenger replied: "Listen, O Lady! You are very arrogant and adamant. Don't challenge my masters, against whose might the universe shudders in fright. They, who have browbeaten the gods and have thrown them out of Heaven, are very powerful. You are a mere woman, and you cannot match their might. Follow my advice and come with me to accept their proposal. Or else you shall be pulled by your hair and taken to their feet."
The Goddess replied: "Whatever you say may be true. Maybe your Shumbhu is so powerful and your Nishumbhu is so virile but I am committed to my pledge. But go now and explain the whole situation to the Demon-lords. Let them come and defeat me!"
Sugreeva then went to his masters Shumbhu and Nishumbhu and explained the whole situation at length. Shumbhu and Nishumbhu became angry and they sent another demon Dhoomralochan to fetch her. But a mere loud cry and wrathful gaze of the Goddess was enough to incinerate the demon Dhoomralochan. The lion of the Goddess slayed the accompanying demons. Then the Demon kings sent Chanda and Munda with a large army to capture the Great Goddess. They encircled the Himalayas to nab the Goddess. The Goddess then produced a black figure of frightening form, called Kaali-Devi or Kaalika Devi. She destroyed the demons easily, hacked off the heads of Chanda and Munda and brought them to the Goddess Ambika. Since she had hacked off the heads of Chanda Munda, she became famous as Chamunda Devi.
Hearing the death of Chanda and Munda, the Demon Kings sent another huge army headed by seven commanders. To match their combined strength the seven gods: Brahma, Vishnu, Shiv, Indra, Mahavaraah, Nrisingh, Swami Kartikeya dispatched their forces. Seeing the temerity of the demons, another beam of power in the form of a woman emerged from the Goddess's body, who sent Lord Shiv as her messenger to Shumbhu and Nishambhu with the message: "If you want your welfare, return the realm of gods to gods along with their right to perform yagyas, and you must now go down to Paataal Lok (Nether world)"
Shumbhu and Nishumbhu refused to accept the Goddess's advice and leading a huge army of terrible demons, reached the battlefield. Supported by the divine powers, the Goddess began to massacre the demons. At that time the demon forces were led by a demon, Raktabeeja. He had the power to reproduce as many demons of his form and dimension as the drops of his blood which fell to the ground. After a fierce battle the Goddess ordered Chamunda (Kali Maa) to spread her mouth far and wide and swallow Raktabeeja alongwith his blood. Chamunda did exactly that and hacked off the head of demon.
Kali Maa then devoured the slain bodies of the asuras and danced a fierce dance to celebrate the victory. This dance of destruction began by Kali and her attendants continued for long and none could stop her. To stop her, Shiva himself mingled among the asuras whom she was annihilating. Shiva allowed himself to be trampled upon by her in this dance of victory because this was the only remedy left to bring her to senses and to protect the world from total annihilation. When Kali Maa saw that she was dancing over the body of her husband, she put her tongue out of her mouth in sorrow and surprise. She remained stunned in this posture and this is how Kali is shown in images with the red tongue protruding from her mouth.
Durga Maa then fought the demon Nishumbhu who was slain in no time. Now Shumbhu decided to take on the Goddess (Durga Maa) himself. Reaching the battlefield, he said to the Goddess: "You take pride on others' strength. Why don't you show your own power!"
The Goddess replied with a smile: "Fool! The whole world is just Me. All Creation is my form in a variety of dimensions. I am the cause and effect of everything: all things emerge from me only and ultimately' enter me only. The whole world is in harmony with My Being."
Then after the nine celestial powers (Kali Maa being one of them) which had emerged from the Goddess (Durga Maa) went back into her and she single handedly killed the demon Shumbhu.
Another source of the origin of Kali Maa is the Adhyatma Ramayana. This text gives another story. It says that when Rama returned home with Sita after destroying Ravana, he boastfully narrated the stories of his victories to Sita. She smiled and said, "You rejoice because you have killed a Ravana with ten heads. But what shall you do with a Ravana with one thousands heads?" Rama very proudly boasted that he would destroy that demon too. Rama accepted his wife's challenge and collected his and his allies army and heads out for Shatadvipa, the abode of this new demon with one thousand heads, who was a powerful demon. When attacked he discharged three magic arrows from his bow. One of these sent all the monkeys to Kishkindhya, their place of residence; another sent the army of Vibhishana, who was an ally of Rama, back to their region beyond seashore, while the third arrow sent all soldiers of Rama back to Ayodhya, Rama's capital. Rama felt humiliated and then Sita laughingly assumed the form of terrific Kali. She proceeded to attack this demon. After a long fight she killed the demon, drank his blood and began to dance and toss about the limbs of his body. It was Shiva who calmed her down. This story, however, has not received popular approval.
II Jai MahaKaali II