I think people beat themselves up – which is different from healthy guidance of oneself (which includes appropriate winces of remorse or shame) – for two reasons: too much inner attacking, and too little inner nurturance. These two forces in the mind are out of balance. Why? Multiple reasons, including individual differences in temperament (some people are more prone to anxiety or grumpiness). But for most people the primary sources are what they have internalized (especially as a child) from their family, peers, and culture. Then, once harsh self-criticism has been internalized along with insufficient internalization of self-nurturance, beating oneself up can take on a life of its own, both as simply a habit and as a way (that goes much too far, at considerable cost) to avoid the possibility of making mistakes or looking bad in front of others.
Whole networks of neurons and related and complex physical processes (e.g., neurotransmitter activity, epigenetic processes) are the basis for acquiring fears, including because a person has been on the receiving end of much anger from others. In other words, learning occurs: emotional, social, somatic, motivational, attitudinal learning: enduring changes in neural structure or function due to a person’s experiences. Check out Joseph LeDoux and the learning of anxiety and fear.
The amygdala also flags experiences as personally relevant, with a bias in most people’s brains toward flagging what is negatively relevant. Then the hippocampus gets involved, tagging that relevant experience for storage. (I’m simplifying a complex process, that also involves other circuitry in the brain.) The amygdala and hippocampus have receptors for various neurochemicals, including oxytocin, and over time these subcortical parts of the brain (two of each, on either side of the brain) can be modified by our experiences; in effect, they “learn,” too.