Pakistan was founded through democratic process but against the popular will of Hindu majority of the subcontinent. They have never accepted Pakistan with open heart and arms. Since Independence, newly born Pakistan found newly born India as a hostile neighbour. Indian occupation of Kashmir, the then princely state resulted in three wars between the two countries. Due to Indian hostility Pakistan was obligated to make friends with anti-Indian forces at the regional and international level, which includes China and the Taliban regime.
Pakistan supported the Taliban movement to end the internal clashes and infighting between different warring groups in Afghanistan. The stable Afghanistan was a necessity for stability in Pakistan. The anti-Taliban lobby (the then Northern alliance) was led by Ahmed Shah Masood and his allies.
They were anti-Pakistan and were the supporters of India. Supporting Taliban was a good move at that time. However, after 9 / 11, the US administration exerted pressure on the General Musharraf to support them in its effort to eliminate the Taliban regime. Musharraf was a dictator and he had no other option but to adhere to the instructions of Bush administration.