The Ramchal has a short essay on reward and punishment at the end of his book, Derech Hashem, the Way of G-d.
It is well known that one of the Thirteen Principles of Faith of the Rambam, is that G-d rewards all good deeds, and punishes the bad deeds. This is viewed as one of the foundations of Judaism. Hashem expressed His confidence that His justice would be taught by Abraham to his children.
The Ramchal’s explanation of how this system of justice works, is very insightful. Because it is a Divine system, it is very complex, and not easily understood by man. The important point to know is that it is absolutely just, and that we cannot understand it.
There are certain good and bad deeds, whose rewards and punishments, are felt immediately. There are others that may take a while to be seen, according to whenever Hashem decides. And there are other instances where the reward and punishment will not be seen at all in this world. This justice will come after we leave this world.
We should not be deterred by צדיק ורע לו רשע וטוב לו, where it appears that the wicked prosper, and the righteous suffer. Our responsibility is to be the best people we can be, and have the confidence that absolute, perfect justice will come, as G-d sees fit.