The concept of the Law-Chaos as appearing in fantasy worlds dichotomy originated with Poul Anderson's Three Hearts and Three Lions, although the underpinnings are much older. Law was what humanity and civilization represented - the imposition of an order on savage, unpredictable wilds - while Chaos was what most non-human things thrived upon: the strange, the weird, the random, and above all, the magical.
Michael Moorcock adapted this, although the interpretation of it grew as his universe expanded. In his earlier works Chaos was essentially antagonistic, even to Elric, who drew his power from it; Law were basically the good guys, albeit ambiguously so (imagine living in a village in France in 1944: Nazis are Chaos, definitely bad guys, but the Allies are Law, and your well-being is for them somewhat secondary to fighting their enemies). Over time, the idea of Balance emerged as an alternative to either side, and although in D&D terms it is 'neutral', in Moorcock's writings it basically became the "good" side and emphasis was put on the reflective similarities of Law and Chaos, at which point they're both basically "evil" (at least once Elric fights a demon and is surprised to learn that it is a demon of Law, not Chaos), but this development didn't occur until well after the D&D system was established.
In the first edition of D&D, Andersonian terms used, but were essentially stand-ins for Good and Evil. PCs were supposed to be Lawful, so Chaotic monsters were the ones you were supposed to fight and Lawful monsters the ones to be friendly with. Supplement I: Greyhawk
had an implicit separation of good from evil, presumably to encourage Chaotic PCs in order to make the newly-introduced Paladin class seem to have more restrictions, but this wasn't made explicit until 2nd edition D&D. When AD&D was first released, D&D went back to the simple Lawful-Neutral-Chaos model and, for the first time, good and evil were fully fleshed out as principles and possible player character alignments.
Roger Zelazny's Chronicles of Amber series also makes use of a Law vs Chaos dichotomy, although it is introduced too late to have a defining influence on D&D, it reflects the characteristics of the two as they appear in AD&D: there are numerous good and bad characters on both sides of the Amber/Courts of Chaos conflict.
With each new product or edition the definitions of the alignments evolved slightly, but it always remained complex and confusing or unsuitable for many people, leaving a lot of variant interpretations and house rules (I, for one, am not satisfied with any printed description of Lawful Evil - that should be where a killer with a sense of honor is found, but the rules never support that interpretation). In the end the philosophical differences between law and chaos were discarded, so that Lawful Good meant "exceptionally good" and Chaotic Evil "exceptional evil"; many people already played as if that were the case.
 They can be traced to ancient Greek philosophy, with positive concepts like logos and kosmos (order and reason) opposed to negative ones aporia and khaos (confusion and chaos). Nietzsche, with other German scholars of the 19th century, identified these two modes as Apollonian and Dionysian, the idea being that human nature was a merger of civilized Apollonian tendencies with the wild, animalistic Dionysian ones. Some also read similar ideas into the Epic of Gilgamesh.
Interestingly, the Egyptians had a concept of order but two concepts of chaos; one, represented by the god Seth, is the kind of chaos found in markets and nature, that which sometimes causes some destruction but is also the source of creativity and new growth, and the other represented by the serpent Apep, is the violent, primordial chaos that only destroys. What makes this arrangement interesting is that Seth was the protector of order (in the form of Ra) against Apep.