This question is one of the most central questions asked by the human species. We've asked both what wars should be waged and how wars should be waged repeatedly.
I've always been of the mindset that one should always try to exceed the behavior of the bad guys, if only by a little bit. While being better than a bad guy may not be enough, it's at least something. That will be an important theme to bear in mind in this analysis.
The most obvious case is when someone faces an apocalyptic threat to one's existence. Another country invading or with an imminent threat would be a key clear case of when war is, to the opinion of all but the most ardent pacifists, justified. There, one is choosing between one's own life and someone else's. Either way, someone is going to die: there's no reason that one's opponent dying is morally superior unless one literally values one's life as lower than someone else's. At that point, a group of people who respond to this apocalyptic threat and keep their opponents alive, let alone deigning to allow one's opponent to mostly maintain their independence, is to be far greater than the "bad guy". Such a defensive war is pretty much the gold standard for justified wars.
One can even extend this to threats that are realistically going to become apocalyptic and world-shaking. None of the Axis powers may actually have been realistically likely to invade the U.S. in the short term, but the fascist powers had all demonstrated that their aggression knew no bounds and that they thought nothing of truly horrific crimes like the Holocaust and the rape of Nanking. While such a war is not strictly defensive, the threat is so clearly inevitable as to justify action.
After that, issues get murky.
Most of us, including the United Nations charter, accept that there is a right to use force to stop ongoing or imminent injustices against us. If one country invades another, pacifies its resistance, imposes a dictatorial regime, but otherwise kills no one aside from those they are in direct battle with, that is still a crime that justifies resistance and war.
The problem, of course, is that what constitutes such an injustice is hotly debated. Is a tax rate that some find too high, or going to programs they disagree with, an injustice? Is economic inequality an injustice, or is imposing greater equality the true injustice?
Still, I think that there is actually a lot more widespread agreement on these issues than one might imagine. I think most people intuitively accept the notion that no person should be impoverished to the point of starvation, lack of shelter, or lack of access to clean drinking water (or an appropriate substitute). Moreover, most people accept that massive inequalities are fairly toxic to society, and imposing them usually requires some injustice.
At the same time, I think few would argue with the implicit position of the American revolutionaries as well as those who have practiced civil disobedience since Thoreau: even great injustices should not inspire war unless there are no non-violent paths left to pursue. War and violence should be the absolute last resort, and even then should be measured, appropriate and just.
When belligerents can negotiate, when win-win scenarios can be constructed, that is obviously preferable to war. Cooler heads should prevail.
Similarly, some magnitude of humanitarian crisis should necessitate the use of force. Standing idly by while civilians are butchered, people are starving, or there is the mass use of rape or torture comes uncomfortably close to complicity.
Again, though, the question is when to intervene and how. Consider this: By the above maxim, one could argue that, in World War II, third parties would have had a moral obligation to prevent the U.S. from engaging in mass bombings like the ones that produced the death tolls in Dresden, Hamburg, Tokyo, Hiroshima and Nagasaki. Yet the U.S. argued that such attacks, even on civilian targets, was necessary to destroy the infrastructure of a belligerent.
For this reason, it makes sense that certain humanitarian interventions are done in a way that attempts to be non-political: noncombatants are given refuge, alleged combatants are released only if there is clear and compelling evidence of a crime and if those intervening can be sure that they will receive a fair trial, humanitarian aid and medical care is made available, but there's no actual involvement in the war.
Finally, it's relevant to consider that the mere fact of a crime or injustice is actually not a justification for war. Any solution to a problem must be justified as at the very least doing more good than harm, and in most instances given an uncertain world we tend to want the margin of good versus harm to be clearly massive.
Those who argue that we should intervene violently in any crisis because it's better than nothing are effectively saying that we should intervene to stop a husband from beating his wife by shooting the wife. The regrettable fact of complex human interactions is that conflict and violence often are deeply ingrained, and it's not easily possible to give people prosperity, liberty or democracy with violent means. Sometimes, one has to do nothing: Just as with an addict who has to be allowed to hit rock bottom, sometimes a society has to hit rock bottom.
Worse, as U.S. citizens are tragically learning today, the good motives of a populace don't matter much if there are enough warmongers and racists tarnishing their good name and if their military has previously lost credibility and trust. One of the great tragedies of the Iraq War is that many people in America would love to be able to do something about ISIS, yet must face the fact that the U.S. military simply has no credibility in stopping them given that it was a previous dishonestly-waged war that to a large degree produced those monsters.
And that notion of credibility is especially central. The fact is that we have no governments of angels. Those of us considering when war is justified often forget that we aren't blameless ourselves. Even in this review, as much as I've tried to avoid it, there's a lot of analysis in there that implies that some nations are just better than others, that some people are just more advanced than others. This is a dangerous line of thinking.
We have to learn how to help each other as a species from a place of solidarity rather than superiority. We have to help our fellow man precisely because we are as flawed and weak and scared as they are, and for the grace of God there we would go.