Evolution by natural selection.
Malaria selected for the HbS trait because those who carried this trait were more likely to survive malaria infection and therefore pass on this trait to their offspring.
Note, of course, that when the carrier rate increases, there is an increased risk that two carriers will procreate and then there will be a 1 in 4 chance that each offspring will inherit HbS alleles from both parents which is lethal, but there’s a 2 in 4 chance their offspring will be carriers and further perpetuate the trait in their local population.