That is probably the most commonly asked question of all time (except for maybe "where's the bathroom?"). A lot of the times it's a little more specific, like "why do bad things happen to good people" or "why do bad things happen to me?". Well, the Gospel answers all form of that question, from why do bad things happen in general, to why do bad things happen to specifically you.
Today, I'd like to quote the Prophet Helaman from the Book of Mormon. In the opening verses of chapter 12 of the Book of Helaman, he writes:
1And thus we can behold how false, and also the unsteadiness of the hearts of the children of men; yea, we can see that the Lord in his great infinite goodness doth bless and prosper those who put their trust in him.
2Yea, and we may see at the very time when he doth prosper his people, yea, in the increase of their fields, their flocks and their herds, and in gold, and in silver, and in all manner of precious things of every kind and art; sparing their lives, and delivering them out of the hands of their enemies; softening the hearts of their enemies that they should not declare wars against them; yea, and in fine, doing all things for the welfare and happiness of his people; yea, then is the time that they do harden their hearts, and do forget the Lord their God, and do trample under their feet the Holy One—yea, and this because of their ease, and their exceedingly great prosperity.
3And thus we see that except the Lord doth chasten his people with many afflictions, yea, except he doth visit them with death and with terror, and with famine and with all manner of pestilence, they will not remember him.
Basically, what's going on here is the people had forgotten their God, and had become a wicked people, so the Lord caused that they should be in wars and many people died. Helaman then went to the Lord and asked for the wars to be replaced with famine, and the Lord did just that. Then, the people, starving, remembered the Lord their God and repented, and because of that the famine was lifted from the land. Well guess what? After they started prospering again, they became prideful and once again forgot their God.
That is why bad things happen. We rely on the Lord for everything. As Helaman later goes on to point out, "the children on men... are less than the dust of the earth" (Helaman 12:7). So, if we're less than the dust of the earth, what have we to be prideful of? Why would we be so quick to forget our God, when everything we have is from him and by his grace? It's when we turn from our Lord when bad things happen. We see it everywhere: movies, TV, sports. Everywhere, there is moral corruption that is proof that we are turning away from our God, and when we do that, the Lord chastens his people to remind us who's truly in charge. So don't forget who's in charge. Don't forget where everything comes from. We're here because of Him, and we're here for Him.