Karma is one of the most commonly misunderstood or misrepresented concepts within buddhadharma. It is often presented as a sort of moral principle founded on a cosmic notion of justice, namely, that everyone gets what is coming to them. In fact,
Karma is simply cause-and-effect.
Everything has a cause (more properly, causes) and is, in turn, a cause of other things. Every cause has effects and those effects are then causes for more effects, etc.
The beginningless and endless cause-and-effect is referred to as the chain of causation.
Obviously, everyone doesn’t get what they “deserve,” whatever that means. There are plenty of people who do a lot of really terrible things and live really great lives. It is so obvious and troubling to our sense of justice that it tends to give rise to the vulgar view of reincarnation, but that will be a future Dharma Thought.