The Senate has passed legislation to reopen the government, bringing the longest shutdown in history closer to an end after a small group of Democrats struck a deal with Republicans.