Everyone gets their appropriate due in this world. Though it sometimes seems as if innocent people are being punished or wicked people are being rewarded, this is only because outsiders often misevaluate the righteousness of a person, or misunderstand the nature of their compensation.
Misperception of Righteousness
A so-called wicked or righteous person might not always be what they seem. Thus, wrong-doers might have merits that justify their rewards, while the righteous might have committed sins which justify their punishment.
The terms צדיק and רשע – R. SaadiaHaEmunot VeHaDeiot 5:2-3About R. Saadia Gaon points out that people are viewed as good or evil based on the majority of their actions. Thus a "צדיק" might still have sinned, and a "רשע" might have done some good.1
צדיק ורע לו – When hardships befall righteous people, it can be assumed that Hashem is punishing them for whatever sins that they committed.2 These sins might not be evident to all for several reasons:
Accidental wrongdoing – According to Ramban, suffering often befalls righteous people for their accidental misdeeds. These are not severe enough to require punishment in the World to Come, but nonetheless need to be atoned. He suggests that in the time of the Mikdash a sin offering would have sufficed, but since its destruction, suffering serves as a replacement.
Sin of omission – R. Bachya suggests that the sin might be one of omission, such as refraining from rebuking those who deserve punishment and abetting them to repent.
Sins in secret – Other sins might be done behind closed doors, or involve thoughts rather than actions.
רשע וטוב לו – The inverse of the above is also true. A wicked person's good deeds might merit him reward, even if such deeds are not obvious to outsiders. As above, such actions might have been done in secret, or before the person earned a reputation for wickedness.
Lack of punishment or reward – Though this position explains why evil people might deserve occasional rewards, and the righteous sometimes need to suffer, it does not explain cases where the wicked appear to never get punished for their obvious misdeeds and the righteous appear to never get rewarded for their positive actions:
Most of these sources suggest that these ultimate rewards/punishments will come only in the World to Come, bringing this approach very close to position #2 below.
One could say, however, that despite appearances, everyone does get their just due. Since Hashem does not punish / reward immediately, it is sometimes hard to see how every deed is paid for measure for measure, but over one's lifetime things do even out. Thus, for example, R. Saadia3 suggests that at times Hashem holds off punishment knowing that a person is to repent,4 or conversely, so as to compound the sinner's punishment later.5
Hashem's providence – According to this approach, there is individual providence, and Hashem is aware of all of an individual's deeds and compensates them appropriately.
World to Come – This position maintains that
Afflictions of love – Ramban understands that all affliction of love come as a punishment for a sin.
Prophetic complaints – Ramban explains that the prophets were not questioning whether Hashem was acting justly, rather it was a way of complaining about their fate.6
Iyyov – The story of Iyyov seems to contradict this approach, since we don't hear from the book that Iyyov committed any sins. Ramban understands
Collective punishment – This approach believes that every man is punished for his own sins. The fact one person might have done something wrong does not require anyone else besides himself to be punished.
"פֹּקֵד עֲוֺן אָבוֹת עַל בָּנִים"
Verses in Torah which promise rewards
Misperception of Retribution
What appears to be a reward or a punishment might actually be the opposite. As such, what is construed as unjust retribution is really not so.
Sometimes Hashem causes the righteous to suffer a little in order to avoid him from suffering a lot.
רשע וטוב לו
Sometimes Hashem keeps the wicked person alive in order for them to witness an even worse punishment than they would have witnessed.
In some cases, the good is a cause for bad, and the whole purpose really is the punishment. For example by Paroh, Hashem did not punish him in order so he will experience the splitting of the sea which was a harsher punishment than he would have received.
Moral justification
Hashem's providence – According to this approach, not all good and evil in life is necessarily a punishment or a reward.
World to come
Afflictions of love
Prophetic complaints – R. Saadia suggests the prophets trusted Hashem he is directing the world justly, rather they were just asking how and for what purpose he was doing what looks to be unjust.7
Iyyov
Just Compensation in the World to Come
People do not get their just compensation in this world. It is only in the World to Come that Hashem gives everyone their appropriate due.
צדיק ורע לו – The righteous man is suffering in this world unjustly, so in the world to come he will get an abundance of good.
רשע וטוב לו – The wicked man being part of a nation who is worthy of good, receives good as well, even though he does not deserve it.
Moral justification – In the world to come, Hashem will give the real reward to the righteous and the real punishment to the wicked.
Hashem's providence – According to this approach, Hashem provides on a national level, and not for each individual person.
Why does Hashem do this?
Natural order – Hashem does not purposely wrong people, rather he lets the world act on its own, and therefore some people in this world do not deserve what happens to them.
Test – R. Saadia explains Hashem's purpose to be a test in order to publicize the commitment of the righteous man even with all the troubles he goes through, and seeing the unworthy retribution the wrongdoer received. Hashem is testing the righteous man to see if he will continue to follow the right path even though he is compensated wrongly. He knows they will tolerate the punishment they don't deserve, and later on in their life or in the world to come, He gives them an abundance of good.
Hashem is using the wicked man for a different purpose before he punishes him, such as to fight with a different nation.
The righteous man is suffering over the sins of his generation, or of his ancestors, and not his own sins.
Iyyov – R. Saadia understands Iyyov's suffering to be a test,11 and as we read at the end of the book, Iyyov got an abundance of good at the end of his life.12
Purposes of the Egyptian bondage – This approach can understand the Egyptian bondage to have an educative or formative purpose rather than being a punishment for a sin.
"פֹּקֵד עֲוֺן אָבוֹת עַל בָּנִים"
World to come – This approach is based heavily on the idea of retribution being given in the world to come, however, the whole concept of the world to come is not explicated in the Torah at all.13
Afflictions of love – This approach understands that Hashem sometimes afflicts out of love to the person in order to multiply their reward, and not as a punishment over a sin.
Not All Receive Just Compensation
People who do not merit Divine providence might not get just compensation. As their lives are ruled by nature, sometimes rewards or punishments are given to the undeserving.