Moshe Rabbeinu (our teacher) asked the same question in a little different way. He asked, "Why do bad things happen to good people?"
The Baal Shem Tov taught that everything comes to teach us something. He said, "Everything we see, we see for a reason."
I explained, "Whenever something comes to you, you have to ask, 'Why did G-d send that to me?' Try to see what you did to bring that particular thing to you, and then adjust your ways accordingly. G-d is not cruel. He does not send things just to hurt us. In fact, what we think are punishments really come to correct the way we are going. So what we experience, whether good or bad, has to come as a continuation of the deed that caused it. This means that we can look at the result and see what similar action brought it about.
Many years ago, very early morning at the Kotel a young American asked me why G-d had someone steal his wallet at the Hostel.
He said, "They took all my money, $250! I came all the way from America for the Holiday… Why would G-d do such a thing?"
I said, "Maybe you stole $250 from someone and you had to pay it back?"
He said, "No Way! I never stole from no one!"
"Maybe you owed that much in charity that you didn't pay?"
He went, "OH! Wow!"
I said, "You are very lucky that Hashem took that from you now when you had it, instead of asking for it after you leave the world, when you stand before the Judge and can't pay it back."
So it is with everything that comes to us, the good and the bad. It all comes to guide us in the direction that Hashem wants us to go.