Law of war
Early sources and history: The first traces of a law of war come from the Babylonians. It is the Code of Hammurabi, king of Babylon, which in 1750 B.C., explains its laws imposing a code of conduct in the event of war: I prescribe these laws so that the strong do not oppress the weak. In ancient India, the Mahabharata and the texts of Manou's law urged mercy on unarmed or wounded enemies. The Bible and the Qur'an also contain rules of respect for the adversary. It is always a matter of establishing rules that protect civilians and the defeated.
Attempts to define and regulate the conduct of individuals, nations, and other agents in war and to mitigate the worst effects of war have a long history. The earliest known instances are found in the Mahabharata and the Old Testament (Torah). In the Indian subcontinent, the Mahabharata describes a discussion between ruling brothers concerning what constitutes acceptable behavior on a battlefield, an early example of the rule of proportionality:

One should not attack chariots with cavalry; chariot warriors should attack chariots. One should not assail someone in distress, neither to scare him nor to defeat him ... War should be waged for the sake of conquest; one should not be enraged toward an enemy who is not trying to kill him.
An example from the Book of Deuteronomy 20:19–20 limits the amount of environmental damage, allowing only the cutting down of non-fruitful trees for use in the siege operation, while fruitful trees should be preserved for use as a food source. Similarly, Deuteronomy 21:10–14 requires that female captives who were forced to marry the victors of a war, then not desired anymore, be let go wherever they want, and requires them not to be treated as slaves nor be sold for money.
In the early 7th century, the first Sunni Muslim caliph, Abu Bakr, whilst instructing his Muslim army, laid down rules against the mutilation of corpses, killing children, females and the elderly. He also laid down rules against environmental harm to trees and slaying of the enemy's animals:

Stop, O people, that I may give you ten rules for your guidance in the battlefield. Do not commit treachery or deviate from the right path. You must not mutilate dead bodies. Neither kill a child, nor a woman, nor an aged man. Bring no harm to the trees, nor burn them with fire, especially those which are fruitful. Slay not any of the enemy's flock, save for your food. You are likely to pass by people who have devoted their lives to monastic services; leave them alone.
In the history of the early Christian church, many Christian writers considered that Christians could not be soldiers or fight wars. Augustine of Hippo contradicted this and wrote about 'just war' doctrine, in which he explained the circumstances when war could or could not be morally justified.
In 697, Adomnan of Iona gathered Kings and church leaders from around Ireland and Scotland to Birr, where he gave them the 'Law of the Innocents', which banned killing women and children in war, and the destruction of churches.
Apart from chivalry in medieval Europe, the Roman Catholic Church also began promulgating teachings on just war, reflected to some extent in movements such as the Peace and Truce of God. The impulse to restrict the extent of warfare, and especially protect the lives and property of non-combatants continued with Hugo Grotius and his attempts to write laws of war.

Modern sources: The modern law of war is made up from three principal sources:

Lawmaking treaties (or conventions)—see § International treaties on the laws of war below.
Custom. Not all the law of war derives from or has been incorporated in such treaties, which can refer to the continuing importance of customary law as articulated by the Martens Clause. Such customary international law is established by the general practice of nations together with their acceptance that such practice is required by law.
General Principles. "Certain fundamental principles provide basic guidance. For instance, the principles of distinction, proportionality, and necessity, all of which are part of customary international law, always apply to the use of armed force."
Positive international humanitarian law consists of treaties (international agreements) that directly affect the laws of war by binding consenting nations and achieving widespread consent.
The opposite of positive laws of war is customary laws of war, many of which were explored at the Nuremberg War Trials. These laws define both the permissive rights of states as well as prohibitions on their conduct when dealing with irregular forces and non-signatories.
The Treaty of Armistice and Regularization of War signed on November 25 and 26, 1820 between the president of the Republic of Colombia, Simón Bolívar and the Chief of the Military Forces of the Spanish Kingdom, Pablo Morillo, is the precursor of the International Humanitarian Law. The Treaty of Guadalupe Hidalgo, signed and ratified by the United States and Mexico in 1848, articulates rules for any future wars, including protection of civilians and treatment of prisoners of war. The Lieber Code, promulgated by the Union during the American Civil War, was critical in the development of the laws of land warfare. 
Historian Geoffrey Best called the period from 1856 to 1909 the law of war's "epoch of highest repute." The defining aspect of this period was the establishment, by states, of a positive legal or legislative foundation (i.e., written) superseding a regime based primarily on religion, chivalry, and customs. It is during this "modern" era that the international conference became the forum for debate and agreement between states and the "multilateral treaty" served as the positive mechanism for codification.
The Nuremberg War Trial judgment on "The Law Relating to War Crimes and Crimes Against Humanity" held, under the guidelines Nuremberg Principles, that treaties like the Hague Convention of 1907, having been widely accepted by "all civilised nations" for about half a century, were by then part of the customary laws of war and binding on all parties whether the party was a signatory to the specific treaty or not.
Interpretations of international humanitarian law change over time and this also affects the laws of war. For example, Carla Del Ponte, the chief prosecutor for the International Criminal Tribunal for the former Yugoslavia pointed out in 2001 that although there is no specific treaty ban on the use of depleted uranium projectiles, there is a developing scientific debate and concern expressed regarding the effect of the use of such projectiles and it is possible that, in future, there may be a consensus view in international legal circles that use of such projectiles violates general principles of the law applicable to use of weapons in armed conflict. This is because in the future it may be the consensus view that depleted uranium projectiles breach one or more of the following treaties: The Universal Declaration of Human Rights, the Charter of the United Nations, the Genocide Convention, the United Nations Convention Against Torture, the Geneva Conventions including Protocol I, the Convention on Conventional Weapons of 1980, the Chemical Weapons Convention, and the Convention on the Physical Protection of Nuclear Material.

Purposes of the laws: It has often been commented that creating laws for something as inherently lawless as war seems like a lesson in absurdity. But based on the adherence to what amounted to customary international humanitarian law by warring parties through the ages, it was believed by many, especially after the eighteenth century, that codifying laws of war would be beneficial.
Some of the central principles underlying laws of war are:

Wars should be limited to achieving the political goals that started the war (e.g., territorial control) and should not include unnecessary destruction.
Wars should be brought to an end as quickly as possible.
People and property that do not contribute to the war effort should be protected against unnecessary destruction and hardship.
To this end, laws of war are intended to mitigate the hardships of war by:

Protecting both combatants and protected non-combatants from unnecessary suffering.
Safeguarding certain fundamental human rights of protected persons who fall into the hands of the enemy, particularly prisoners of war, the wounded and sick, children, and protected civilians.
Facilitating the restoration of peace.
The idea that there is a right to war concerns, on the one hand, the jus ad bellum, the right to make war or to enter war, assuming a motive such as to defend oneself from a threat or danger, presupposes a declaration of war that warns the adversary: war is a loyal act, and on the other hand, jus in bello, the law of war, the way of making war, which involves behaving as soldiers invested with a mission for which all violence is not allowed. In any case, the very idea of a right to war is based on an idea of war that can be defined as an armed conflict, limited in space, limited in time, and by its objectives. War begins with a declaration (of war), ends with a treaty (of peace) or surrender agreement, an act of sharing, etc.

Principles of the laws of war: Military necessity, along with distinction, proportionality, humanity (sometimes called unnecessary suffering), and honor (sometimes called chivalry) are the five most commonly cited principles of international humanitarian law governing the legal use of force in an armed conflict.