The extension is necessary because lawmakers have been unable to complete work on the 12-must pass spending bills required to keep the various arms of the federal government running each year.
With the federal government's fiscal year in its waning hours, all non- essential parts of the government would have been required to shut down at midnight had the extension not been agreed to.
The Senate voted 62-38 to agree to the extension.
House lawmakers voted strongly in favor of the measure last week.
President Barack Obama must sign it into law by midnight to avert the shutdown.
The extension was attached to one of the spending bills - legislation to fund Congress and its related agencies. It is the first of the dozen bills that lawmakers have finished.
As such, Democrats were accused by their Republican counterparts of putting Congress' interests before the rest of the country's. This bill was most likely completed first as it is the least controversial of the 12 pieces of legislation.
The minority party was also critical of the package because it includes language allowing the U.S. Postal Service to defer a $4 billion payment to its employees' pension fund.
The taxpayer will make the payment on behalf of the postal service, which would make up the shortfall over the next several years.
The Democrats defeated an attempt by the Republicans to strip out the postal service language.