Roman infantry tactics, whether manipular or cohortal, were build around a number of key points:
First, the pilum was designed to break up enemy formations, whether charging Gauls or more stationary opponents being charged by the Romans, right before the moment of impact. Part of the pilum's design was that its head, after penetrating an opponents shield, would lodge itself there, forcing that warrior to either drop the shield or attempt to fight with one hand essentially tied behind his back. As your average Gaul wore no armor, loss of his shield left him with no protection.
Secondly, the Romans employed a large shield - the scutum - and a short stabbing sword - the gladius. This combination allowed them to maintain much tighter formations than those who relied on longer slashing blades as did the Gauls. A unit of Romans, in maniple or cohort, could thus stand shoulder to shoulder and still wield their weapons to full effect. If packed in as tightly, Gauls and the like would be to constricted too properly hack and chop.
Thirdly, the combination of scutum and gladius, along with Roman drill and discipline, enabled reserve units to directly relieve units already engaged by passing through their ranks in good order. So, as pawpaw mentioned, if the hastati were in danger of being overwhelmed, or simply needed a breather, the principes could filter through the ranks of the hastati to engage the enemy while the hastati withdrew in good order. The same applies to the reserve centuries of a cohortal legion reinforcing and relieving their frontline comrades.
Finally, a Roman legion rarely fought alone. Not only were there usually more than one legion operating in unison, they almost invariably went into battle with an assortment of light troops and cavalry. These light troops - which would have included the velites in the manipular legion, mercenary slingers such as the Balearics and archers such as the Cretans, and the various numerii and symmacharii light troops of the early empire - would deploy forward of the main battleline and fall back as they shot up the advancing enemy. A legion would also, in all likelihood, have deployed artillery in the form of ballistae or onagers to start killing and demoralizing the barbarous hordes before they even came within charge range.
The barbarians, on the other hand, lacked the tactical flexibility, and usually the morale, to drive home their initial charge in most cases. As the best and bravest warriors strove for a place in the front ranks, those behind were usually less skilled and/or less enthusiastic. Therefore, if the initial charge failed to carry the day, a barbarian horde would come apart as the front ranks died spitted upon Roman pila or gutted bu Roman gladii while the less committed fled.
Obviouslt, this didn't always play to script (Trebia,Lake Trasimene and Cannae, Teutoberger Wald, the Dacian War, Septimus Severus' failure to subdue the Caledonians among others). But it worked often enough to build and maintain the empire until the growing number of mobile, horse-based adversaries forced the Roman military to evolve in the early Third Century.