Geoffrey of Monmouth's Historia Regum Britanniae. Earlier accounts describe a far different Merlin.
The original Myrddin had nothing to do with Arthur. The earliest Welsh poems that concern the Myrddin legend present him as a madman living a wretched existence in the Caledonian Forest, ruminating on his former existence and the disaster that brought him low: the death of his lord Gwenddolau, whom he served as bard. The allusions in these poems serve to sketch out the events of the Battle of Arfderydd, where Rhydderch Hael king of Rheged slaughtered the forces of Gwenddolau, and Merddin went mad watching this defeat. The Annales Cambriae date this bate to AD 573, and name Gwenddolau's adversaries as Gwrgi and Peredur, the sons of Eliffer.
A version of this legend is preserved in a late fifteenth-century manuscript, in a story called Lailoken and Kentigern. In this narrative, St. Kentigern meets in a deserted place with a naked, hairy madman who is called Lailoken, although said by some to be called Merlynum or "Merlin", who declares that he has been condemned for his sins to wander in the company of beasts. He added that he had been the cause for the deaths of all of the persons killed in the battle fought "on the plain between Liddel and Carwannok." Having told his story, the madman lept up and fled from the presence of the saint back into the wilderness. He appears several times more in the narrative until at last asking St. Kentigern for the Sacrament, prophesying that he was about to die a triple death. After some hesitation, the saint granted the madman's wish, and later that day the shepherdss of King Meldred captured him, beat him with clubs, then cast him into the river Tweed where his body was pierced by a stake, thus fulfilling his prophecy.
Welsh literature has many examples of a prophetic literature, predicting the military victory of all of the Celtic peoples of Britain who will join together and drive the English -- and later the Normans also -- back into the sea. Some of these works were claimed to be the prophecies of Myrddin; some of it was not, as for example the Armes Prydein.
It was Geoffrey of Monmouth who introduced Merlin into the mythos of King Arthur. He slightly altered Myrddin's name to Merlin to avoid a resemblance to the obscene French word merde. While Geoffrey is remembered most for his character of Arthur, it was Merlin whom he concentrated on, making the prophetic bard the central character of his three books: Prophetiae Merlini, Historiae Regum Britanniae, and Vita Merlini. As a result of this introduction, Merlin became a subordinate character to Arthur, and as the Arthurian mythos was retold and embellished upon, Merlin's prophetic aspects were de-emphasized in favor of portraying Merlin as a wizard and elder advisor to Arthur. In the later medieval stories of Merlin, his eventual downfall came from his lusting after a woman named Nimue, who coaxed his magical secrets from him, eventually turning the magic she had learned upon him against him, and imprisoning him in an oak tree. This was unfortunate for Arthur, depriving him of Merlin's counsel when he most needed it.
A variation of Merlin was seen in T.H. White's Arthurian retelling, The Once and Future King, in which Merlin has the curious affliction of living backwards in time to everyone else. Dan Simmons used this notion in his Hyperion, the young woman Rachel in this book suffering the "Merlin Sickness", growing progressively younger and forgetting her life day by day.
In most modern popular interpretations such as in fantasy literature and superhero stories, Merlin is depicted as a wizard of ultimate power.