A shutdown can last as long as it takes Congress to agree on a spending bill and for the President to agree to sign it. As of 2014, the longest government shutdown in history lasted three weeks. Usually, however, shutdowns last only a few days.
When Congress comes to an impasse regarding budgets and expenditures, or if the president vetoes such a bill presented to him by Congress, the government must, by law, shut down until an agreement is reached. This is because, in order for the government to spend tax money, there must first be legislation that dictates its allocation.