Why does God allow children to die?
On the one hand, clear knowledge of "why" is often kept from us. While we do believe that everything in this world is a planned and perfect expression of God's positive and creative will (Talmud Brachos 60b), we, as human beings, just can't see it all.
Indeed, the limits to our flesh-and-blood vision are part of the message of the final chapters of the book of Job. And by designating separate blessings to be said upon hearing good news ("...Who is good and bestows good") and on hearing bad news (where God is characterized as "...the true judge.") our Sages taught us that, while tragedy and blessing come from the same infallible Divine source, in this world we - all of us - understand them differently.
In other words, there's a recognition of the basic nature of the human condition that, while we may express our faith that all is indeed for the best and that God is the true and perfect judge, nevertheless, in this world of darkness, we will never see its details in their full clarity.
On the other hand, the darkness isn't complete. Some possible explanations do exist within Torah thought. Take, as an example, the concept of gilgul neshamos - transmigration of the soul. You won't find this idea in the Talmudic literature because it was, until recent centuries, part of a hidden kabalistic tradition. The earliest printed reference to the idea (of which I'm aware) is in Nachmanides' essay on the book of Job (written around 800 years ago).
Here (according to my most limited understanding) is how it goes: There is a finite number of Jewish souls, each of which has been charged with a specific mission. Until a soul reaches perfection in its mission, it hasn't done its job and might be sent down to this world for another try in order to make up for what was previously left out. If a child should tragically die young, it might well be that it's because his soul's purpose was completed in these few years of life and has now been returned to its source enveloped in brilliant success.
One might also look at a short life from the perspective of the impact it left on the world. Some people spend 80 years thinking of nothing but themselves and their personal enjoyment and leave precious little behind them as a legacy. But others seem to inspire thoughtfulness and sensitivity in all who meet them. Their lives are lessons in fine traits like decency and humility and their deaths might also bring out acts and thoughts of goodness and compassion. A life like this can be worth more than another's full 80 years, can't it?
Why do bad things happen to good people?
The general question "why do bad things happen to good people" pre-supposes two things: (1) that we know which things are "bad" and, (2) that we know which people are "good."
No one's expected to love death. So it's reasonable if we don't fully see death as good thing (even though it is...see Psalm 116; 15 and remember Rabbi Akiva in the Talmud who said "all God does is for the best"). Nevertheless, on an intellectual level, we can appreciate that, since God has a complex and constructive design for the world (and is all-powerful and all-knowing), then a particular event - even a death - isn't bad at all; it just looks that way as a result of our limited understanding.
Our rabbis teach that suffering in this world can help "clean" our accounts and thereby enable us to fully enjoy the benefits of our this-worldly righteousness in the next world. The rabbis (Brochos 5b) declared "chavivim yesurim" ("beloved is suffering" - even though we personally refrain from seeking suffering). It is also taught that death itself can provide immeasurable atonement. So how are we to know what's really bad?
Now, who is a good person? How are we to judge others in any absolute sense? Their thoughts and most of their actions are hidden from our eyes. Most of us have a hard enough time just coming to grips with our own failings. Granted, the Torah requires us to judge others in the best possible light (Levit. 19; 15), but that surely doesn't mean that we're to assume everyone's perfect! Eccl. 7; 20 would certainly seem to suggest differently.
At root, then, the question is no question because we just don't know what's bad and who's good. What you can ask, though, is why did God create the world in such a way that suffering is required. Couldn't He have done things a bit differently?
One approach to that problem can be found in the Mesilas Yesharim (by the early 18th Century scholar Rabbi Moshe Chaim Luzzato): Man was created essentially to bask in the warmth and goodness of the "Divine light" (i.e., the next world). But in order to fully appreciate that goodness, one must have earned the opportunity first. This is done through the self-perfection of Torah study and mitzvos. These activities prepare our bodies, souls and personalities for closeness with God in a way that can't happen in this physical world.
However, our tasks being so complex and we, by nature, being so lazy and distracted, we need inspiration and guidance (not to mention correction and rebuke) to keep us, as they say, on task. One of the tools best suited to this guidance is suffering. God could, indeed, have given the pleasure to us for free, but that would fall into the category the kabalists call "nehama d'kesufa" (bread of shame) - something not properly earned. That's something whose pleasure isn't nearly as great.
Facing imminent death
Let me begin by quoting a lengthy passage from the Talmud (Brachos 10a) where the Jewish king, Chizkiya is described as facing a most serious illness:
"...God brought suffering upon Chizkiya and said to (the prophet) Isaiah to go visit the sick king, as it says (II Kings 20) "and in those days, Chizkiya became deathly ill (Isaiah 38) and Isaiah ben Amotz the prophet came to him and said: â€˜thus says God: command your household for you will die and you will not live.'" Why (asks the Gemara) does it say (both) "for you will die and you won't live?" (that is to say) that he will die in this world and will not live even in the next. Chizkiya asked: "why all this?" (Isaiah) answered: "because you didn't (marry and) procreate" Chizkiya replied "that was because it was shown me through divine inspiration that (evil) children would come from me." (Isaiah) answered: "what business do you have with the secret plans of God? What you were commanded (by the Torah) simply do (i.e., you should have married and had children anyway) and whatever is proper before God He will bring to be." (Chizkiya) answered: "Alright. You give your daughter to me in marriage and perhaps your merits and mine will combine to produce good children." (Isaiah) replied: "the decree has already been declared against you (i.e., you will certainly die)." (Chizkiya) answered: "Ben Amotz! Finish up your prophecy and go, for I have a tradition from my ancestor (King David), 'even if a sharp sword is upon a man's neck, don't give up hope of receiving divine mercy.'"
I believe that this passage teaches that we are in God's hands throughout our lives - even in the most difficult times and even when hope is extinguished, and that, indeed, hope is never extinguished (allowing for the fact that not every story has a visibly "happy" ending).
While there are many ways to approach the fear of death (and of life itself), I think the most profound and (when mastered) also the most successful, is trust in God. This trust isn't the simplistic belief that God will somehow jump to every snap of our fingers or that we can force Him to fill our every desire, but it is the belief that He is in control; that He can do anything; that whatever He does do is ultimately for the best - even if we can't see how right now.
By the way, the king Chizkiya lived many years after this "final" illness...demonstrating not that it had to be that way, but that it could turn out that way (for any of us).
There are many, many passages of the Torah that address these thoughts. Let me offer the following few for now... Psalms chapters 23, 27 and perhaps 130.