How does stimming work? As in, how does it help self-regulate? Is it that it shifts one's focus from unpleasant stimuli?
Stimming works in several ways. The simplest is that sometimes it is just fun. It evokes the same feelings of joy and happiness anything else that is pleasant. This generally referred to as a sensory diet - experiencing sensory input for the sake of the experience.
It can also act as a coping tool in that it is used for sensory or emotional self regulation. This works several ways, but the most common is using a stim that induces a calming effect. For example, I find that petting my stuffed fox is extremely relaxing. When I am emotionally overloaded, I can pet my stuff fox and it will have a grounding effect on me because the tactile sensation calms me down.
Another type of regulation is sensory self regulation. This is using a stim specifically to manage your sensory environment. An example of this might be that in a crowded restaurant with a lot of noise I will take my tablet out and play a game that involves a lot of tapping on the screen. The immediate tactile sensory experience of the taping occupies more of my attention than the sounds around me.
Yet another type of stimming is cognitive self regulation. This is particularly common in ADHD. Someone with hyperactive-impulsive traits might bounce their foot or leg to get rid of excess energy so they can pay attention to a lecture. An inattentive ADHD might use a fidget to take up extra cognitive processing that would otherwise focus on random things around the room (this is why I stim in class; without a constant stim to take up my processing, my brain will latch on to sounds around me and I get distracted).
There are other types of stimming, I’m sure, but these are the big ones that come to my mind as someone who is autistic and ADHD inattentive type with a few hyperactive-impulsive traits (though not enough for a combined type diagnosis).