Basically the Roman maniple (legion was the name for a section of army equivalent to a battalion today) replaced the phalanx due to higher mobility on the battlefield.
Though a macedonian phalanx was a perfect defense formation it lacked mobility and as a result was relatively inable to confront any attacks made on the sides or rear.
This immobilty allowed the romans to easily perform flanking maneuvers on the battlefield clearly exampled in the Macedonian Wars.
Source: Polybius; The Histories, Book XVIII, Chapters 28-32
Someone can probably give a more in-depth answer, but Polybius' argued that it came down to the maniple's greater flexibility, pointing out that if the phalanx was unable to force the enemy to attack head-on on perfectly flat ground then it wasn't very useful.