Why do bad things happen to good people?
And, even if they do, bad things, not good things, should happen to bad people!
If only good things happened to good people and bad things to bad people then there would be a big limitation on free will.
Also, people aren't pure or evil. A vast majority of people have some good traits and actions and some that are not as good, so it is impossible to say one person is good and another bad. Lastly, ultimate reward and punishment are not in this world, so what you see as good might be really bad and vice versa.
(Based on Avot 4:19)
Here's an excerpt from the Shaar Bitachon which addresses this: http://dafyomireview.com/article.php?docid=380
If one asks: Behold we see some tzadikim which must work very hard to earn a livelihood while many people who rebel against G-d live a good life in comfort? The answer to this was already addressed by the prophets and the chasidim (extremely pious). One of them said "You are right, O L-ord, when I contend with You, but I will argue with You. Why has the way of the wicked prospered, all who deal with treachery have peace?" (Yirmiyahu 12:1), and another "Why do You show me iniquity and look upon mischief; and plunder and violence are before me; and the one who bears quarrel and strife endures." (Chavakuk 1:3), and "Therefore Torah is slackened, and justice does not go out forever, for a wicked man surrounds the righteous; therefore, justice emerges perverted." (Chavakuk 1:4), and "[You are] of eyes too pure to behold evil, and You cannot gaze upon iniquity. Why should You gaze upon traitors, be silent when a wicked man swallows up one more righteous than he?" (Chavakuk 1:13), and another one said "Behold these are wicked, yet they are tranquil in the world and have increased wealth." (Tehilim 73:12), and "But for naught I cleansed my heart and bathed my hands with cleanliness" (Tehilim 73:13), and another said "And now we praise the bold transgressors, those who work wickedness are built up, they tempt G-d, and they have, nevertheless, escaped." (Malachi 3:15), and many more like this.
But the prophet refrained from giving an answer because each specific case has its own particular reason. Therefore Moshe Rabeinu commented on this in the torah saying (Devarim 29:28) "the hidden things belong to G-d"..and "the Rock, His deeds are perfect for all His ways are just" (Devarim 32:4).
WHY THE RIGHTEOUS SOMETIMES SUFFER Nevertheless, I would like to clarify the matter to some extent. The possibilities why a tzadik is prevented from obtaining his livelihood without effort and must instead be put through the system of efforts and tests is as follows. 1. A previous sin for which he is now obligated to pay for. "the tzadik will pay in the land" (Mishlei 11:31)
As an exchange to pay him more good in Olam Haba. "to benefit you in your end" (Devarim 8:16)
To demonstrate his good acceptance of difficulty in the service of G-d, so that other men will learn from him, as you know from the story of Iyov.
Due to the wickedness of his generation, G-d tests him with poverty or sickness to demonstrate his piety and service of G-d despite them, as written "Indeed, he bore our illnesses, and our pains he carried them" (Yeshaya 53:4).
Due to his not standing up for G-d's torah and exacting justice from men of his generation, as you know from the story of Eli and his sons, as the verse says "And it will be that everyone who is left in your house, will come to prostrate himself before him for a silver piece and a morsel of bread" (Shmuel 2:36).
WHY THE WICKED SOMETIMES PROSPER Sometimes G-d sends good to the wicked for the following reasons: 1. A previous good deed he did, to pay him in this world, as written "And He repays those He hates to their face, to destroy them" (Devarim 7:10) which the Onkelos renders: "He pays those He hates their good deeds during their lives to destroy them".
As a temporary deposit, until G-d gives him a righteous son who is worthy of it, as written "he prepares but the tzadik will wear it" (Iyov 27:17), and "For to a man who is good in His sight, He has given wisdom and knowledge and joy, but to the sinner He has given a preoccupation to gather and to accumulate, to give to him who is good in G-d's sight; this too is vanity and vexation of spirit." (Koheles 2:26).
Sometimes the money is the greatest cause of his evil (in the next world) or death (in this world), as written "There is a grievous evil that I saw under the sun; riches kept by their owner for his harm." (Koheles 5:12).
Sometimes it is to give him time to repent and become worthy of it.
His father did good and it is fitting to benefit him in the merit of his father, as said to Yehu ben Nimshi "four generations of your descendants will sit on the throne of Israel" (Melachim II 10:30), and "He who walks innocently is righteous; fortunate are his sons after him." (Mishlei 20:7), and "I was young, and have aged, and I have not seen a righteous man forsaken and his descendants seeking bread." (Tehilim 37:25).
Sometimes it is to test those who are deceptive or have an evil interior. When they see the wicked prosper, they quickly stray from the service of G-d and hasten to win the favor of the wicked and to learn from their actions. In this way it will be clarified the pure men to G-d and will be demonstrated who was faithful to G-d in bearing at a time when the wicked rule and persecute him. He will receive reward from the Creator for this, as you know of the story of Eliyahu and Isabel or Yirmiyahu and the kings of his generation.
Why do good things happen to bad people, so that they can be rewarded in this world for any good they might have done, leaving only punishment in the world to come. As Kind David says (Psalms 92:7-8):
ז אִישׁ-בַּעַר, לֹא יֵדָע; וּכְסִיל, לֹא-יָבִין אֶת-זֹאת.
7 A brutish man knoweth not, neither doth a fool understand this.
ח בִּפְרֹחַ רְשָׁעִים, כְּמוֹ עֵשֶׂב, וַיָּצִיצוּ, כָּל-פֹּעֲלֵי אָוֶן: לְהִשָּׁמְדָם עֲדֵי-עַד.
8 When the wicked spring up as the grass, and when all the workers of iniquity do flourish; it is that they may be destroyed for ever.
The opposite is also true. Sometimes G-d gives bad to good people in this world, so that they can have only good in the world to come.
Sometimes, in order for good people to be rewarded in this world, G-d takes some of the reward they were due in the world to come (as illustrated in the story of Chanina ben Dosa and the golden table leg).