I was watching some videos about the Roman-Macedonian wars, does anyone know why the Legion was able to best the Phalanx? Was it due to superior cavalry on the flanks? It doesn't make much sense to me since the Phalanx should theoretically be able to out range the Legion.
Sometimes, Roman legions beat the phalanx, and sometimes the phalanx beat the Romans. In the Macedonian Wars, the Romans did well in most of the major battles (which, of course, contributed to their winning the wars), but phalanx-based armies under Hannibal and Pyrrhus inflicted multiple defeats on Roman armies.
Faced with the prospect of facing the pike phalanx, the Romans had two choices:
Adopt the pike phalanx themselves. If the Romans could match the enemy phalanx in numbers, training, and discipline, they would have a good chance in battle. However, the challenge of matching the world-leaders of phalanx warfare in training and discipline should not be underestimated.
Do something different.
In their wars against Carthage, Rome faced a similar problem at sea: how could they defeat the Carthaginian fleet? Faced with the choice between trying to outdo Carthage in seamanship or trying something different, they tried something different (the corvus and boarding-based tactics). Similarly, against the phalanx, they tried something different.
Leaving aside the auxiliary forces on both sides (the light infantry, missile troops, and cavalry) and only looking at the core of the armies, describing Rome versus the phalanx as "sword-and-shield vs pike" leaves out a very important component. A more accurate description would be "sword-and-shield and javelin vs pike". While the pike provides an advantage in reach over the sword, the javelin outranges the pike. The javelin (pilum) gave Roman infantry the chance to hit back at the phalanx without needing to get in close. If they did get in close, sword-and-shield is much handier in very close combat than a long pike.
Roman infantry have another advantage over the pike phalanx: flexibility. As Polybius wrote about the Roman defeat of Macedon:
the Macedonian phalanx is difficult, and sometimes impossible to handle, because the men cannot act either in squads or separately.
and
The Roman order on the other hand is flexible: for every Roman, once armed and on the field, is equally well equipped for every place, time, or appearance of the enemy. He is, moreover, quite ready and needs to make no change, whether he is required to fight in the main body, or in a detachment, or in a single maniple, or even by himself. Therefore, as the individual members of the Roman force are so much more serviceable, their plans are also much more often attended by success than those of others.
Together, these give a chance of victory against the phalanx. When it doesn't work, it's usually straightforward: the phalanx steamrollers the Roman legions. How does it work when it works? In the Battle of Cynoscephalae (197BC), the Macedonians and Romans were each winning half of the infantry battle. The Macedonian right wing was doing an excellent job of pushing the Romans back. However, the Macedonian left wing did not have a chance to properly form up (the battle was a meeting engagement rather than a set-piece battle) and was falling back as the Roman right attacked it. Roman flexibility, enabled by low-level initiative, led to the Roman right detaching some units to attack the rear of the Macedonian right, and this decided the battle.
In the Battle of Magnesia (190BC), each side won the cavalry engagement on the respective right flanks. This resulted in slingers and archers shooting at the Macedonian phalanx from the Roman right. While Roman infantry could have sent some detachments to keep such missile troops away, the phalanx lacked that flexibility, and began an ordered retreat. However, the elephants stationed with the phalanx panicked under the arrows and slingshot, and broke their own phalanx, and that was the end of that.
The Battle of Pydna (168BC) was a hard-fought set-piece battle. The Roman infantry attacked the Macedonian phalanx, repeatedly and without success. The phalanx then proceeded to push the Romans back. As the Romans fell back before this attack, the phalanx became disordered (partly due to rough terrain, and presumably also partly due to incoming Roman javelibns), and gaps opened in it. A Roman counterattack exploited these gaps, and once the Roman infantry closely engaged the phalanx, it became a Roman victory.
At Cynoscephalae and Pydna, Roman victory depended on Roman tactical flexibility allowing the Romans to get in close to the phalanx, where Roman infantry had a decisive advantage. At Magnesia, it was more a matter of the inflexibility of the phalanx causing the phalanx to retire and collapse.
Of course, the Romans faced threats other than phalanxes in battle and siege, and the Roman choice of equipment and tactics reflected their varied enemies. The classic Roman legion was not specifically an answer to the phalanx, even though these battles show that it could, at times, beat the phalanx.