You know the situation: you do everything you're supposed to do, you do everything the right way...and then bad things happen to you. How is that fair? Why do bad things happen to good people?
This is a tough one to answer since it requires giving what might seem to be bad news (does that make me the bad thing happening to a good person reading this?), but the answer is that behind this question is a misconception of how the universe works. The universe follows fundamental laws, such as inertia and every action has an equal but opposite reaction. Within that universe, social animals follow ingrained ethics, which include fairness, reciprocity, and minimal harm (to yourself and the people you care about). Ethics evolved in humans to live cooperatively in society—i.e., to grow more safely and effectively as a group without hurting anyone within it (which would break down trust and destroy the group). Now that humans have this inherent and socially instilled sense of ethics, they think that the universe should follow them. It does not.
The interesting thing is that a person would still think the universe was unfair even if it was fair, and it'd be impossible for the universe to seem fair to everyone. The reason is that all judgments of “good” and “bad” are relative and subjective based on the judge. For example, you could treat others poorly without noticing, and you would think that something bad happening to you was unjust when the people who were treated poorly would think that you deserved it very much. Different perspectives of the same situation would lead to different conclusions, so it would still seem unfair to you (even if it was fair), and it's impossible for everyone in that instance to agree on the event's fairness.
Additionally, most events can have good and bad aspects to them, and you can choose to look at different aspects of it to draw different conclusions. For example, you could make the argument that the COVID-19 pandemic was great for the environment or that it made you realize how valuable family time was. So what might seem like a “bad thing happening to a good person” could actually be one of the best things to ever happen to them. Alternatively, someone going to jail for committing a crime, which we would call a "bad thing happening to a bad person" in this analysis, could actually be a "good thing" if it does set them straight and cause them to find a new, better path for themselves and others.
Similarly, a bad thing happening to you (and, let’s be honest, almost no one really thinks they’re a bad person—they can always blame their behavior on a circumstance that caused them to behave in that way) could be the thing that led you to find out what you really wanted in life and gave you true meaning and purpose in life. If that’s the case, is that not a bad thing happening to a good person that was actually a good thing? So how do you even keep track of what’s good or bad if it's so open to interpretation and what you end up making of the situation?
You can see where this goes logically. Shakespeare wrote that nothing is inherently good or bad, but thinking makes it so. Most events have both positive and negative aspects to them. Therefore, good things will happen to bad people, and bad things will happen to good people—events occurring at random will do that, and all events have some sort of pros and cons that you weigh differently based on what you want at the time.
Consider that when you go to the kitchen to get food. What it good or bad that you had to exert effort (is effort "bad"???) to get the food. You valued the food more than the effort, so you saw it as a good thing. If you had to walk 500 miles for a piece of fruit, though, you’d judge that as bad. You might see having someone bring it to you as "good" from your perspective since you didn't have to exert that effort, but what if the person who brought it to you was forced to do so? Is it good then? Of course not. So you’re constantly judging the goodness or badness of situations based on the conditions and factors involved.
Knowing the above, it now seems almost ludicrous to think that if you are nice that the world will magically deliver nice things to you because you "deserve it"—as if you're cashing in your "ethics chips" with the world. One brutal example of people thinking that way are “nice guys” who think that if they’re kind to women for long enough that they will earn enough credit with them to receive sex in return. We all know that while being a nice person is generally a prerequisite to a romantic relationship, it is insufficient to achieve the goal by itself.
So, in life, you may follow the social rule to treat others as you want to be treated and hope that they reciprocate, and you can hold expectations for others to behave in the same way and punish or cut off communication with them if they don't. But it's not reasonable to then assume that you're entitled to great random events because the universe doesn’t work that way. If it did, you would get punished for things you that didn't realize you did because people don’t always perceive things in the same way.
That all being said, there are two things within your control to make the odds favor you as much as possible:
You can mitigate the risk and impact of negative events through your own actions: moving away from high-risk areas for negative events, buying insurance, protecting yourself, avoiding people who don't reciprocate your kindness and generosity, etc.
By practicing the above two habits, you can maximize the good outcomes you see from random events and from other people and minimize the negative events, behaviors, and outcomes.
(note: some philosophies believe that by doing the above two things, there is a non-physical impact as a result, such as Karma or the Law of Attraction making you more likely to see things in the world that reflect your mindset and resulting actions; in those cases, the above still holds but you would just add a third bullet to think good thoughts and hold positive expectations about other people and the world around you)
However, that doesn’t mean that by being a good person, you can go gamble and win every bet because the universe owes you. You don’t have a universal ethical score or currency that you can cash in when you need it. Rather, you’re a good person because you know that others have no reason to be to you if you are not to them, and you mitigate the risk of random negative events so you can minimize their impact.
As you can see, examples of "fairness" based on whether other people are being fair or reciprocating, not the universe. Random things do happen, and all you can do is minimize the risk and impact of those things happening. Finally, "good" and "bad" are heavily based on perception, as fire can cook your food or burn you to death.
So once you've done all you can to minimize bad things happening in your life, all that is left within your control is to focus on what you want/like and don’t focus on what you don’t. So if you’re a good person and someone still mistreats you, then all you can do is try to make sure that it doesn't happen again and then get back to your life. But the universe has some randomness, and all events have pros and cons. Therefore, you’ll always be able to find “bad things happening to good people” if you look for them, and it’s not as nonsensical as people make it out to be for the universe to work that way. It’s simply a conflation of human ethics and universal forces, which are completely different concepts. Don't let bad events get you down, and don't use them as an excuse to continue the cycle ad infinitum. Focus on your growth, and you'll keep making progress regardless of what other people do or what random events occur.