A single sentence in a town record from Hamelin, Germany dated 1384 provides the only primary-source we have for an incident which remains utterly mysterious. History knows only that the town lost its children, and has no idea how.
Folklore, of course, provides an answer, and one with far more detail than the record bears out: this notation in the archives is the first written telling of the tale that would become the Pied Piper of Hamelin.
There was one other very early account of the story, and one that introduces the story in a form closer to that known today: a stained-glass window in the town's church, described as depicting a piper in colorful clothing leading the children away. (The window has been mentioned in a number of sources, but does not seem to have survived past the 17th century.) A source from ca. 1440, only a century and a half after the incident, specifies the date as June 26th, 1284, and indicates that the piper led away 130 children.
A German chronicle from the 16th century introduces the version of the story we all know today - that the piper was hired to lure away a plague of rats, and then stole the children when he was not paid.
We don't know what happened. There are interesting guesses, but that is likely all they will ever be. We do know, however, how the story has been depicted in the seven centuries since whatever reality lies behind it, and our understanding of the early tellings is shaped by which theory we believe. For example, it has been proposed that the piper was a literal child predator, though this is not taken seriously by most historians. The more important question is what he represents, both in the past and today.
One hypothesis holds that the children of Hamelin left and founded another town, perhaps in Transylvania (this is included in the Brothers' Grimm version even with the magical piper). The Piper in this case would originate as a description of some landowner who had recruited them, or possibly even purchased orphans. Another idea, which I consider more likely, is that the children died of natural causes, perhaps in some epidemic. The image of death as a musician leading them away is then an early manifestation of the totentanz motif common in later medieval art, in which death leads a procession of dancers from all walks of life, showing that our ultimate fate is equal regardless of station. The sad irony of whatever tragedy befell Hamelin is that it did not take all lives equally, and so the musician was instead shown leading only children out of town.
Within a few generations, the story was that a piper had come, in brightly-colored clothing, and taken the children. Without any more details, we don't know how the story was told, but it must have been a frightening tale - the piper seems to show up without reason, and 130 children are lost. (In some early versions they are specifically killed, with one account saying they were taken to the hill where executions were carried out.) The addition of the rat plague motif must have tempered the story, making it into a cautionary tale about making good on promises and adding some apparent logic to the monstrous actions of the piper.
Today, everyone is used to associating the story with a plague of rats. (In a few modern German tellings, the ratcatcher doesn't even carry a pipe, but lures the rats out by some unspecified means.) The rat has become a kind of mascot for the town of Hamelin, which sells a great deal of rat-themed merchandise. Modern reimaginings often focus on the rats, such as Pratchett's The Amazing Maurice and his Educated Rodents, Mieville's King Rat, or the Grimm episode the legend inspired. Not one of these has a plot which primarily depends on anyone's refusal to pay, suggesting that - although that theme is still certainly present in straight tellings - that theme is far less central to how we imagine the story today. The monstrous quality of the Grimm and King Rat versions (and the similar reputation of Pratchett's piper, who turns out to be a fraud) highlights the current vision: the Pied Piper seems to have become a devilish figure whose power first seems to be directed to the townspeople's will, but quickly proves to be beyond their control, capricious, and even sadistic. Considering that many early accounts made the piper out to be the literal devil, and before that he may have represented death, it seems to fit the origins perfectly.