There are no simple answers to this question. Some of these answers might appeal to you. However, none of these theories are necessarily correct or incorrect. You decide.
In the distant past people believed in many gods. Their theory was that something bad happened to you because you angered one of the gods. Your family or friends perhaps never knew what you had done wrong, but one of the gods knew and decided to punish you.
In the Old Testament, Moses told Aaron that good people encountered bad situations because they were good. God will test you, to test your faith, such as depicted in the story of Job.
An atheist might say that everything is scientific. Certain events are falling out in a certain pattern along physical principles, energy moving in a certain direction resulting in various consequences, which we might interpret as good or bad from our perspective as people. Keep in mind as an example, if a person or animal dies there is a benefit to the plants, insects and other animals who might see it as food.
Everything that happens simply happens and we interpret it as good and bad with everybody having our own opinion. Sometimes it’s just bad luck with no deeper meaning.
Another perspective is that God is putting everything in motion and everything is planned out already. There is no Free Will because everything is predestined and we can’t see the big picture because we’re only people —however God has a master plan which we need to believe in.
The perspective of Karma is that somewhere along our life we did something good or bad and at another time in the future we will harvest the results of that good and bad. And these events can be completely unrelated…. but there is a Great Balance.
A version of this is that a bad thing that we did could be in a previous lifetime, in a previous reincarnation, and we harvest the bad luck in this time —— even though we can’t remember what we did wrong in a previous life.
Another point of view is there are people, because of their upbringing, or because of some genetic flaws in the brain, are cold- hearted people. They lack compassion to others and are basically selfish. If they have an opportunity to do harm to another to benefit themselves they will do it. They can be intelligent, educated ( or not), but they are able to justify their lack of empathy by making some sort of excuse about their behavior. They might say to themselves: “That person is from another country , or from another religion, or is a man, or is a woman, or is a different color, etc. —— so they are less than me”.
And therefore whatever they do is OK. They have no moral core.
Also, Bad Things Happen because of ignorance. Some people just don’t understand how something works and make a mistake. Maybe they are young and not careful, or old and handicapped.
And lastly, a Christian perspective is that bad things happen to us so that we will have compassion for others. It makes us a better person.