The Constitution had to be written because the original organization of the new states under the Articles of Confederation failed to turn the individual colonies into a true nation. Under the Articles of Confederation the individual states retained their own sovereignty, freedom and independence from each other and more importantly from a federal government. States were free to issue their own money, fix their own tariffs, and maintain their own militia. There was no president or federal judiciary. There was a need to stop the states from acting in their own self interests rather than in the interests of the states as a nation. The Articles of Confederation were replaced by the US Constitution, creating "a more perfect union." (Even though the prior union was not perfect and technically there is no such thing as "more perfect.")
Mainly, the Constitution provides the framework for our republican form of government. It confers specific authority to each of the branches of government and to the several states. And it provides for certain protections of civil rights that no majority can abridge or abrogate.
It was also a response to Shays' Rebellion.
Daniel Shays led a rebellion burning down court houses throughout the colonies in 1786. The farmers were in a desperate situation and figured if there were no courthouses, the judges couldn't rule to have their farms taken away. It began in Massachusetts and spread to other states. The states did not have the manpower to back down the militia rebellion, but under the Articles of the Confederation the federal government did not have the authority to step in. Many of the men involved in writing the Constitution were actually nervous of giving the federal government more power so soon after they had fought the Revolutionary War. Even George Washington was quoted as saying "I almost despair of seeing a favorable issue to the proceedings of the convention, and do therefore repent having any agency in the business."