The United States suffered their first loss of the tournament at the hands of Lithuania, but both teams had already qualified along with Germany and Slovenia.
The US will play Italy in the last eight, while Lithuania will face Serbia, Germany take on Latvia and Slovenia play Canada.
The US and Canada also qualified for the Paris Olympics as the two highest-placed teams from the Americas, with Puerto Rico, the Dominican Republic and Brazil all bowing out.
Spain and Canada went perfect through the group stage with three wins, but both were on the end of shock defeats when the second round got underway.
That set them up for a winner-takes-all head-to-head clash in Jakarta, and it was Canada who were left standing after a dramatic 88-85 win.
Shai Gilgeous-Alexander, who finished as the game's top scorer with 30 points, said "our group wanted it a little bit more".
"We knew what was at stake and we were willing to do whatever it took, and we ended up getting it done," he said.
Spain were 10 points in front at half-time and increased their lead by two at the end of the third quarter, but Canada came storming back.
Thirteen of Gilgeous-Alexander's points came in the decisive fourth quarter, and team-mate Dillon Brooks helped out with 22 overall.
Canada head coach Jordi Fernandez said his players were "the definition of a winning team".
"We didn't come here just to go to the Olympics," he said.
"We want to make sure that everyone knows that we're knocking at the door and that we're thirsty for more."
Spain head coach Sergio Scariolo said he "couldn't be prouder" of his team but lamented their failure to stop Gilgeous-Alexander.
"It's true that in four quarters, floor leadership is crucial," he said.
"We had to know that when the opponent is an NBA All Star and he's handling it like that, things might happen."
- Latvia's dream run -
Latvia joined Canada in the quarter-finals after thrashing Brazil 104-84.
Latvia are making their World Cup debut and have already beaten Spain and Tokyo Olympics silver medallists France at the tournament.
Head coach Luca Banchi said his team "doesn't need a coach any more" after another hugely impressive performance.
"They are solid, conscious, accurate, bringing more and more confidence into the tournament," said the Italian.
"We can adapt, they know what I'm asking when we go into some choices, they know what's the style that works."
Italy beat Puerto Rico 73-57 to reach the quarter-finals for the first time since 1998.
Head coach Gianmarco Pozzecco, who was a player in Italy's squad 25 years ago, said he had "12 lions on the court".
"I asked them before the game if they want to continue, and they answered on the court," he said.
Serbia beat the Dominican Republic 112-79 to advance, getting back on track after a surprise loss to Italy in their previous game.
Lithuania are now the tournament's only unbeaten team after they beat the US 110-104 to hand the Americans their first loss.
A furious second-half US comeback was not enough to stop a Lithuania team that nailed their first nine three-point attempts.
US head coach Steve Kerr said the Americans would have to start sharper in the quarter-finals.
"We needed to feel this, we needed to respond, and next game we have to start the way we did in the second half," he said.
"We can't ease into the game at all. I'm hoping that this is a lesson and we get better from this."
Germany beat Slovenia 100-71 in a game between two teams who had already qualified for the quarter-finals.
In games between teams who had already been eliminated, Australia beat Georgia 100-84 and Montenegro beat Greece 73-69.