The proposal to construct the 1,100km fence was approved after senators voted 80 to 19 in favour of the plan on Friday.
The bill will now be sent to President Bush to sign.
The House of Representatives – the lower house of Congress – passed the bill two weeks ago.
A separate bill has made $1.2 billion available to pay for the barrier. A 23km section of fence currently under construction in San Diego is costing $126.5 million.
In addition to the funding for the fence, a homeland security bill passed by both the Senate and the House on Friday includes $380m to hire 1,500 more border patrol agents.
It also provides money to build detention facilities to hold 6,700 more illegal immigrants.
John Boehner, the House majority leader and a Republican, speaking to reporters said: “We have made giant steps in terms of our ability to control illegal immigration.”
The spending bill also criminalises the building of tunnels across the borders with Mexico or Canada which senators said could be used by drug smugglers, illegal aliens or terrorists.
Bush had sought broader legislation that would have created a guest-worker programme to help to provide a steady workforce for jobs Americans are either unable or unwilling to do, but he was unable to push it through Congress in the face of Republican opposition.
Opponents of the fence said it would be expensive and would not stop illegal immigration.
Ken Salazar, a Democrat senator from Colorado, said: “This is a political gimmick.
“It is not in the long-term interest of the United States of America and the Western Hemisphere.”
“It may be good politics, but it’s bad immigration policy”.
Edward Kennedy, Democratic senator for Massachusetts
Another Democratic senator, Edward Kennedy, called the fence “a bumper sticker solution for a complex problem”.
“It’s a feel-good plan that will have little effect in the real world,” he said.
“We all know what this is about. It may be good politics, but it’s bad immigration policy”.
Immigration is expected to be a major issue during campaigning before congressional elections on November 7.
Luis Ernesto Derbez, Mexico’s foreign minister, said a diplomatic note would be sent to the US to complain about the fence.
“We will send a note that will respectfully tell the White House the reasons why the Mexican government thinks it’s not the right solution and could be a cause of grievance between the countries,” he said.
An estimated 1.2 million illegal immigrants were arrested in the last fiscal year trying to cross into the United States along the border states of Texas, New Mexico, Arizona and California.
Sections of the fence would be built in each state.