TL;DR: The trope came from the facts that:
- If you were impaled on a spear, the smart way to remove it would be to keep pushing it through your body and out the other side.1 So people who actually fought in the era of swords and spears would have seen it done, albeit in less badass circumstances.
- More importantly, it actually happened sometimes. In fact, it happened so often that some weapons were specifically designed to prevent it...

Example of a boar spear, c. 1430, in the Kunsthistorisches Museum
Also, the Bear Spear (or Bärenspieß), a larger version of the same thing:

Bear Spear, German or Austrian, c. 1435, Metropolitan Museum of New York
In Hunting:
This style of spearhead is also known as a winged, barred, or lugged spear. Below the point at the top of the spearhead, there are two arms (called "lugs") sitting at an angle perpendicular to the line of the shaft and spearhead. These lugs are there for a reason.
The boar spear was already in use by 600 BCE in Persia, but was probably around much earlier:
One of the more common styles of spears that were used up until the spear’s effectiveness decreased, was the winged spear... This style of spear was thought to have been used since as early as 3000 B.C.E. by the Sumerians.
- Worcester Polytechnic Institute
The boar spear was invented - as the name suggests - for hunting boars. Boars are aggressive, dangerous animals that will go to great lengths to kill anything that threatens them. The lugs on a boar spear are actually designed to prevent an impaled boar from working its way up the spear and attack whoever is holding the other end of the shaft.
Persian Cylinder Seal showing boar spear in use, c. 600 BCE, British Museum
The Greek historian and scholar Xenophon left us the earliest known *written reference to a boar spear while describing a boar hunt, circa 400 BCE:
The javelins must be of every variety, the blades broad and keen, and the shafts strong. The spears must have blades fifteen inches long, and stout teeth at the middle of the socket, forged in one piece but standing out; and their shafts must be of cornel wood, as thick as a military spear. The caltrops must be similar to those used in hunting deer. There must be several huntsmen, for the task of capturing the beast is no light one even for a large number of men. I will now explain how to use each portion of the outfit in hunting.
If, in spite of javelins and stones, he refuses to pull the rope tight, but draws back, wheels round and marks his assailant, in that case the man must approach him spear in hand, and grasp it with the left in front and the right behind, since the left steadies while the right drives it. The left foot must follow the left hand forward, and the right foot the other hand.
As he advances let him hold the spear before him, with his legs not much further apart than in wrestling, turning the left side towards the left hand, and then watching the beast's eye and noting the movement of the fellow's head. Let him present the spear, taking care that the boar doesn't knock it out of his hand with a jerk of his head, since he follows up the impetus of the sudden knock.
In case this accident should happen, the man must fall on his face and clutch the undergrowth beneath him, for, if the beast attacks him in this position, he is unable to lift the man's body owing to the upward curve of his tusks; but if his body is off the ground, the man is certain to be gored. Consequently the boar tries to lift him up, and, if he cannot, he stands over and tramples on him.
For a man in this critical situation there is only one escape from these disasters. One of his fellow huntsmen must approach with a spear and provoke the boar by making as though he would hurl it; but he must not hurl it, or he may hit the man on the ground.
On seeing this the boar will leave the man under him and turn savagely and furiously on his tormentor. The other must jump up instantly, remembering to keep his spear in his hand as he rises, for safety without victory is not honourable.
He must again present the spear in the same way as before, and thrust it inside the shoulder-blade where the throat is, and push with all his might. The enraged beast will come on, and but for the teeth of the blade, would shove himself forward along the shaft far enough to reach the man holding the spear.
- Cynegeticus (On Hunting), 10.3-10.17; Xenophon

Early fourth century Roman floor mosaic depicting a boar spear, from the Villa del Casale in Sicily

Illustration featuring a boar spear, 14th century, Italian
Xenophon's warnings have been repeated countless times over the ensuing millennia, by everyone from sportsmen to kings:
A boar at bay is by no means a pleasant creature to contemplate, with his huge neck and bristling crest, his fiery eyes, and his glistening, white and champing tusks, rattling like castanets as they toss off bits of adhesive foam that fleck his brindled sides. It is impossible for those who have never encountered him in his native wilds to realize his fierce and terrible aspect, his lumbering but swift gallop, the bold rush that lifts a horse from the ground, and leaves the imprint of his teeth in its flank as an extended gory rut, or his rage when impaled upon the spear. His last effort is to force himself up the spear-shaft in eagerness to avenge the wound.
- Boars and Boar Hunting by Dr. G. Archie Stockwell, F.Z.S.; 1894
And:
Spears are logical close range hunting weapons. They allow the animal to be stabbed from a relatively safe distance while still allowing a physical contest of strength with the boar. Apparently however the safety of this weapon was not always guaranteed by its length. It seems that hunters from at least the mid-fourteenth century onward found it necessary to put a cross guard like that on a sword at the base of the spear head in order to prevent the maddened, dying
boar from running the spear through itself and progressing further up the shaft and mauling the hunter who held the spear. At first this crossguard was simply a piece of wood tied to the shaft at the base of the spear head, (fig. 1) but later it became integral with the head. (fig 2)

- Boar Hunting Weapons of the Late Middle Ages and Renaissance, by Doug Strong
The famed medieval hunting writer Gaston III, Count of Foix (better known as Gaston Phebus) went so far as to say that if you spear a boar and find he is stronger than you, the best you can do is hold on for dear life and hope that God helps you survive:
If one dares attack a boar in his den one should do so with a ‘barred’ spear, i.e. one that is equipped with a crossbar below the head, or ‘winged’, to prevent the weapon from entering too deep, thus enabling the hunter to keep out of range of the boar’s deadly tusks. For the same reason one should grip the shaft in the middle and not near the head. If the boar is attacking one must not couch the spear but wield it with both fists. But if the hunter has struck one should couch the spear and with all one’s might press against the boar. If the beast is stronger than the hunter, the latter should change from one side to the other but not for a moment relax his hold, and keep pushing until God or men come to his aid.
- Gaston III, Count of Foix, Le Livre de la chasse (The Book of Hunting), 1387
The threat of an angry, speared boar was recognized throughout Europe:
Early Tudor huntsmen used a range of largely specialised weaponry to pursue and kill their prey. For boar, a special spear and sword, or 'tokke', both with cross bars beneath the point designed to prevent over-penetration, were used by huntsmen such as the Duke of Suffolk and the Marquis of Dorset, when they were on embassy to the French Court. Boar hunting would have been reserved to ambassadors abroad, but most huntsmen would have used a hunting sword or woodknife, such as the one with 'the hefte being gilte' that Lord Montegew gave Henry VIII in New Year, 1532. Despite this specialisation, weapons were similar enough to be interchangeable and hunting weapons were occasionally used in real fighting. An Irish soldier was stabbed with a boar spear by a German mercenary in 1544 during an affray that followed Henry VIII's capture of Boulogne.
- Hunting, Hawking and the Early Tudor Gentleman, James Williams; History Today, Aug 2003, Vol. 53, Issue 8
Hunting boar with the spear was so dangerous that, as early as the fifth century, military leaders and tacticians saw it as a useful preparation for military service:
Stag- and boar-hunters may usefully be joined to the military. This is a matter on which the entire safety of the State depends, that recruits be levied who are outstanding both in physique and moral quality.
- Vegetius, Epitoma Rei Militaris (Military Institutions of the Romans), c. 400 (the only Ancient Roman military manual that has survived intact to the present day)
The value of boar hunting as preparation for combat did not go unnoticed even by kings:
...for hunting is a training for all types of fighting met with in war; against a foe crossing in front, in a head-on encounter or in a pursuit; in an awkward situation or a sounder one. For every kind of military encounter, hunting is a better training than jousting. If the tourney teaches a man how to strike with a sword on a helmet, how much better he will learn by striking down a boar when his only chance of saving himself is by a good thrust with the spear?
- King John I of Portugal, Livro da Montaria (Hunting Book of John I), c. 1415-1430
And some instructors of historical forms of combat still believe that hunting boar with a spear is a uniquely useful way to learn how to use a sword and spear properly against human opponents:
Even the most inexperienced Western Martial Arts novice can appreciate the absurdity of equating a toe stab or a wrist flick with a powerful thrust to the head or body in sparring, but some sword games count these as hits of equal worth. When boar hunting, no such inanity applies. Blows of insufficient force or thrusts to non-vital areas are less than worthless, as they serve only to further enrage the boar while exposing the hunter to serious peril.
Like humans, wild boar often require multiple non-survivable wounds before they are incapacitated. Boar have been known to leap back up and continue fighting even after apparently having been killed. Wounded opponents (humans and animals) may continue to move and fight for seconds to minutes after receiving one or more unquestionably mortal wounds. Delivering multiple lethal blows in rapid succession, then employing ones weapon to restrain or hold an opponent at bay (be he boar or man) until he expires of his wounds is a critically important skill that is absent from the western martial arts lexicon.
- Medieval Hunting as Training for War Insights for the Modern Swordsman by Richard Swinney MD, Principal Instructor, Bramble Schoole of Defence; and Scott Crawford, Director, Bramble Schoole of Defence
In Warfare:
Well before the medieval era, these spears were adopted for use in combat against human foes, in part for the same reason they were preferred by boar hunters.
Regular spear:

Boar spear:

If you spear an enemy armed with a shorter spear or a sword, you don't want him to slide up the shaft and hack you to pieces while your hands are occupied holding a weapon that is stuck in his torso and therefore useless to you. You're much safer if the spear is designed in such a way that he can't pull his body up the shaft.
This is especially true if you're a foot soldier (and in the medieval period, spears were for foot soldiers; lances were for horsemen) trying to kill a charging cavalry horse before it tramples you. Horses don't like impaling themselves as much as boars do, but they move so fast that if you spear them while they're galloping towards you, their momentum can easily force the spear deeper into the their body. If this happens, you're dead.
Even if the spear happens to pierce the horse's heart or something and it dies before it crushes you, you're still dead. Remember, there's a knight riding that horse; he has a sword, axe, or war hammer; he's wearing WAY more armor than you are; and he's REALLY mad because you just killed his horse. He raises his weapon to cave in your skull. You try to raise your own weapon to block his blow - then you remember that your weapon is buried 5 feet deep in a dead horse. You have just enough time to say "Ah, crap" before your brains splatter all over the place.
So the lugs should keep the spear from going too deep, and that's a good thing. The other reason for adding them: they made it easier to parry your opponent's attacks, hook and pull away his shield, or snare him and pull him from his horse.
Military variants of the boar spear:
Note that in Europe, especially by the middle of the Medieval era, the names of different kinds of polearms were increasingly being used interchangeably, and even scholars of the weapons of this period have trouble deciding what to call individual examples.
Jūmonji yari
The Japanese equivalent of the boar spear, used in combat by samurai, though it presumably has a history similar to the European boar spear, and likely began as a weapon for hunting, rather than for warfare.

Jūmonji yari, 16th century
Winged Spear

Frankish, c. 1050

Assortment of Viking-era winged spearheads
Other Assorted European Variants:

1If moving forward will get you closer to the guy who skewered you, he must be standing in front of you. That means the spearhead is sticking out your back. That means that the best way to get the damned thing out of you is to push it the rest of the way out your back. If you pull it out the way it went in, the spearhead will have to pass through your body again, which will hurt WAY more than pushing the shaft through your back, and the spearhead will do even more damage to your innards. Push it the way it was already going, and it will hurt less and do less damage.

Note:
There was also a Boar Sword, similar in design and function to the Boar Spear, but this was never used in combat, as far as I can tell:

Boar Sword given to Henry VIII by the Emperor Maximillian as a gift on his accession to the throne in 1508.
Modern replica of a late medieval Boar Sword
By the sixteenth century the boar sword had developed along the same lines as the boar spear. The blade of the sword had become narrow for the first two thirds of its length until at the end of the sword it widened out to normal width. They were usually fitted with cross guards at the end of the wide portion of the blade and these were often spring loaded to pop out into place when necessary. Some of these boar swords had the final blade part made in the manner of a flamberge.
- Boar Hunting Weapons of the Late Middle Ages and Renaissance, Doug Strong
Note: "Flamberge" is a style of sword with a wavy blade outline; something very similar to the design mentioned here is seen in the replica Boar Sword show above this quote.