Every time a virus replicates, errors (mutations) occur in its genetic material. This is how variants arise.
Usually, the changes either have no impact or are harmful to the virus
Where do coronavirus variants come from? Through mutation, new variants of a virus are expected to occur explains an article on CNN.

How Corona Variants Arise

The 30-something woman wasn’t sick for very long. Admitted to the hospital with Covid-19, she needed a little oxygen and was given steroids, but was well enough to go home after nine days. But the virus stayed in her body for seven months, mutating multiple times as it wrestled with her less-than-stellar immune system. This is how coronavirus variants arise.

All viruses mutate constantly if and when they are replicating in a host body. The more people infected, the more chances the virus has to evolve through the process known as mutation.

“The longer somebody has that virus, and the longer that virus has to deal with people’s antibody responses, the bigger the chance is that variants will emerge,” said Penny Moore, an expert in viruses at South Africa’s National Institute for Communicable Diseases. In this case, the patient, a South African, was infected with HIV, the human immunodeficiency virus that causes AIDS. Her HIV was not well controlled and had damaged her immune system, giving the virus the edge it needed to survive in her body for more than 200 days.

In other cases, people may be taking immune-suppressing drugs because they have had organ transplants, or to fight autoimmune diseases. Or they may just have immune systems that are slow to respond to infection.

But around the world, on any given day, the virus is infecting people and mutating in their bodies. When those mutations give the virus some sort of advantage — the ability to replicate faster, or to hide from the immune system — that version will outcompete others.