Sadly it’s almost always “learned” from (or actively “taught” by) others, mostly parents or peers who themselves learned it from parents.
Children/young adults aren’t even necessarily aware that their behaviour is inappropriate as it’s “normal” based on what they’ve seen happen around them their whole lives.
Adults should know better, but may do it anyway due to the human tendency to often dig their feet in, or consciously/actively do it for a reason which really has nothing to with the persecuted group.
Bigotry/racism/sexism persevere largely due to the above.