Three main things:
The phalanx's weakness was broken/uneven ground. Gaps would form. The Roman manipular system built the gaps so moving on uneven terrain was less of a problem and they could exploit the gaps. The Legions found that they had the flexibility to work past the sarissa points.
The Macedonian and Seleucid Greek armies that Rome faced were not the balanced combined arms force that Alexander had commanded. Alexander's army used a well trained cavalry as a hammer with the Macedonian Phalanx serving as the anvil. As the Hellenistic period drew on, these various Greek rulers found it increasingly difficult to afford and man such an army. They opted for maintaining the infantry phalanx at the expense of the other arms. Their warfare increasing became a reversion to the older hoplite phalanx pushing matches, except armed now with the Sarissa.
And lastly, the Greeks were fighting Rome when her Legion's were at their peak in professionalism (210-160 BCE). Most of the early campaigns were fought with veterans of the 2nd Punic War. Very bad timing for the Macedonian and Seleucid armies.
http://legacy.fordham.edu/halsall/ancient/polybius-maniple.asp
Polybius actually went into detail comparing the Roman Maniple formation to the Macedonian Phalanx.
He accepts that the phalanx is almost unbeatable head on and on flat ground due to their many rows of spearpoints and the fact that the phalangites could be packed shoulder to shoulder while the Romans needed to keep sizable gaps in between each man in order to utilize their swords effectively. However, as Polybius puts it:
But if the enemy finds it possible, and even easy, to avoid its attack, what becomes of its formidable character?
The phalanx-based army is rarely able to decide when and where a battle is to take place. And even when they do they can rarely force the enemy to stay in one place nor can they force the enemy to attack along the whole length of the phalanx evenly. As a result, the flexible maniple formation was far more effective in practice.