Back when the war between the monsters and the humans was happening, the human magicians accidentally created a disease when they banished the monsters to the underground. The disease caused life draining crystals of different colors to form on those who were infected, and they spread faster as the monster's hp got lower.
Unfortunately, the entire underground was contaminated with it. All the monsters had the disease.
Somehow, many years later, Chara ended up getting the disease, and was banished to the underground. They were found by Asriel. The two became great friends.
However, about 9 months after Asriel found Chara, Asriel was killed by the disease. His dust was spread throughout the entire underground. Afterwards, Chara took their own life.
Some of the dust got into an Echo Flower. That flower became senate, and thus Flowey was created.
Anyways, Gaster ended up mysteriously vanishing one day. Luckily, all the monsters still remember him. He'd built the CORE and such. He was quickly replaced by Alphys.
Alphys' number one goal was to create a cure for the disease. Sans was her test subject, seeing as he had only one HP. It was a miracle that he was even still alive.
After many failed attempts, she finally created a successful cure. There was enough for every monster in the Underground, but only just.
Needless to say, Sans looked like a mess from the amount of failed cures. But, he's alive.
All the humans that fall into the underground have been quarantined there. They are usually treated like friends, seeing as Asgore has no reason to attack them. They always end up being killed from the disease anyways.