Attempting to create the Antichrist and so bring about the end of days, Satan impregnated a virgin, but, wise to his designs, she ensured that the child would be baptized immediately after its birth. The child was thus a Christian and saved from the power of the Devil, but nonetheless inherited prophetic powers from his diabolical heritage.
So runs one version of the origin story of Merlin, from a text worthy of its own post later. But the Doylist origin of the Merlin character is even more intriguing.
Based on the earliest chronicles, the following appears to be historical: the Romans in Britain were hard hit by the first assaults of the Anglo-Saxon invaders, and those who survived banded together and chose one among their number as their leader, Ambrosius Aurelianus. He seems from the scant sources we have (most of which derive from a brief mention by Gildas) to have been of noble heritage, and to have won a great battle against the Saxons in the fifth century. Save for this, we know almost nothing.
Meanwhile, the Romans had established a fort on the southern coast of Wales and named it Moridunum, from a British word meaning "fort by the sea;" the Britons borrowed this back into their own language (often equated with proto-Brythonic, the common ancestor of the Welsh and Breton languages and several others now lost), calling the place by a name that has become Caerfyrddin in modern Welsh. This sounds like it should mean "Castle of Myrddin" (the f in the full place name being an example of a perfectly ordinary grammatical alternation in Welsh), and so it came to be thought that an important person by that name had once lived there.
In sixth-century Scotland, legends about St. Kentigern tell us he crossed paths with a madman who lived alone in the forest who had the gift of prophecy and foretold the death of the king of Strathclyde. His name was Laleocen or Lailoken, but British legend fused him with the Myrddin of Wales, and he came to be known as "Myrddin Wyllt," said to have been from Caerfyrddin but living in Caledonia.
Myrddin Wyllt is said in legend to have been a bard with a prophetic gift who served his lord faithfully until the disastrous battle of Arfderydd in 573, when the men of Strathclyde massacred the servants of his own lord, Gwenddoleu. Then he fled into the forest and lived as a hermit; his last prophecy is said to have been of his own death.
Geoffrey of Monmouth conflated the characters of Myrddin Wyllt and Ambrosius Aurelianus into one figure, Merlinus Ambrosius (Myrddin Emrys, in Welsh; it's likely that the /l/ in the Latinization is to avoid a pernicious similarity with the Anglo-Norman merde.) Of this compound figure, Geoffrey wrote three accounts: the Prophetiae Merlini or "Prophecies of Merlin" based entirely on Welsh material in which Merlin offers a prophecy about the wars between the Britons and the Saxons; the Historia Regum Brittaniae or "History of the Kingdom of the Britons", which includes the entire Prophetiae preceded by a biographical sketch which casts a hybrid Myrddin/Ambrosius figure as a companion of King Arthur for the first time; and later another text, the Vita Merlini or "Life of Merlin," which is a more thorough biography of the now fully merged character of Merlinus Ambrosius. The Historia is the first source of the notion that Merlin is the son of a demon or of the Devil himself, conceived as an unsuccessful attempt at creating the Antichrist.
Following Geoffrey, the historical background of the real Romano-British warlord Ambrosius Aurelianus was lost from the legend, making Merlin merely an advisor to kings rather than a general. Lost also were certain particulars of the Myrddin Wyllt story, though an un-hybridized version of this seems to have persisted in Welsh folklore into the fifteenth century, when a text called Lailoken and Kentigern was written. In this story, St. Kentigern meets Lailoken, who is explicitly identified with Merlin; Lailoken tells Kentigern his life story, then asks for sacraments as he is about to die by falling, by stabbing, and by drowning. He is shortly afterward driven off a cliff to fall on a stake set by a fisherman in the water below, impaling himself while dying with his head submerged. None of these things make it into Arthurian material about Merlin.
Robert de Boron adapted Geoffrey's narrative into his own poem, Merlin, the text of which has been lost. De Boron's Merlin is very definitely the child of a demon, and has the power to shapeshift as well as foresee the future (a motif that reappears in TH White's modern work), and at the end of his life serves a master named Blaise, who writes down Merlin's deeds. This in turn was adapted into a French prose retelling in the 12th century, now called the "Prose Merlin", which seems to have been treated as canonical by the anonymous authors of the Vulgate and Post-Vulgate cycles, important Old French sources used by Thomas Malory in compiling Le Morte Darthur. These authors added a fanciful new story of Merlin's death (the legendary Myrddin Wyllt's ending having been long forgotten), in which he lusts for a young woman named Niviane or Viviane, teaches her his magic, and then falls victim to her own use of these powers; the precise means she employs varies a great deal from one version of the legend to another.
The result is a legendary figure who has his own tragic place in the grand drama of the Matter of Britain, quite unrecognizable from the sources on which he is based, but one who, in his various legendary incarnations, has taken on a life of his own.