The idea of restraint in war is very ancient. The Old Testament gives guidelines for accepting surrender, for the treatment of captives, and for restraining the destructiveness of war (see Deut 20:1-20, 21:10-14). In times even less civilized than today, many countries had the practice of only allowing a conquering army to loot a city for three days to control the amount of damage done to civilians.
Just war theory has two main guiding principles for just practices in war. The first is obvious: war must be waged against soldiers, not civilians. Civilians must never be targeted. Armies must take great pains to avoid accidentally harming civilians, especially because of the use of explosives. While clear enough, several factors can complicate this criteria. When a country completely mobilizes for war, the civilians end up being a crucial part of the war effort. For example, ordinary factory workers end up being an essential part of producing tanks, airplanes, and supplies. This was the reason why British and American forces intentionally targeted civilian neighborhoods around German factories in WWII. While the Allies had just reasons for going to war, the practice of killing hundreds of thousands of German civilians - men, women and children - to stop the German war effort was certainly not in accordance with just war theory.
The second principle is restraint. During war, armies must use the appropriate amount of force to achieve the desired goals in the war. For example, during the Persian Gulf War in 1991, American forces used enough force to cause the Iraqi army to retreat or surrender. It would have been possible to annihilate the entire army, but that would have been more violent than necessary to achieve the goal of the war. In WWII, the Allies decided that they needed to go further than defeating Axis armies - they needed a complete defeat of the government of Germany. This was likely in accordance with just war principles, because the Nazi government had demonstrated that it was completely incapable of keeping a treaty, and was of course guilty of crimes against humanity.
Why do countries otherwise committed to just war principles occasionally violate them? In any war, some soldiers will violate just war principles and attack civilians. In some cases, enemy soldiers use population centers as shields or use civilians as combatants, making soldiers get trigger happy. In the case of WWII, the Allies decided to target German civilian populations partially in retaliation for Germany's bombing of civilians in London. More importantly, the Allies violated just war principles because they saw what happened to the captive countries of Germany (especially Poland) and decided that they must win, no matter what the cost.
This is one of the reminders of the just war principle that war should always be a last resort. Once in war, nations are often faced with horrible moral dilemmas and may have difficulty staying faithful to other just war guidelines.