Osama Hawsawi gave Saudi Arabia a shock lead and although David Villa and Xabi Alonso scored to turn things around, Mohammed Al-Sahlawi's 74th-minute goal appeared to have denied the European champions. However, Athletic Bilbao striker Llorente netted a minute into time added on to save the blushes of Vicente Del Bosque and his team - tipped by many to challenge strongly for glory in South Africa.
Villa had the first chance of the game in the eighth minute - but his 20-yard shot was well off target. Saudi Arabia responded almost immediately, with Naif Hazazi's effort from the right side of the penalty area going wide of Iker Casillas' goal. Spain defender Sergio Ramos came close in the 13th minute, when he headed Xavi's corner just wide from 12 yards.
However, the Saudis went straight up the other end and were given an opportunity to take the lead when defender Gerard Pique fouled Mishal Al Saeed. Abdoh Autef took the free-kick, but his curling left-foot effort from around 18 yards out hit the Spain wall. In the 17th minute though, Saudi Arabia did make Spain pay as they took a surprise lead.
Spain conceded a corner and Autef's delivery was perfect for Hawsawi to head low past Casillas from 12 yards. Del Bosque's side responded to the setback positively, with Villa bringing an excellent save out of goalkeeper Waleed Abdullah. However, Villa was not to be denied as he brought Spain level just past the half hour mark.
Andres Iniesta's cross from the left was perfect for Villa - and the Barcelona striker clinically headed past Abdullah. Pique could have got on the scoresheet himself two minutes later, but his connection with Xavi's cross was poor and Abdullah saved easily. Xavi was the provider soon after, as Ramos hit the post from 12 yards. Then in the 38th minute Villa was off target from 18 yards.
The Saudis held on until half-time - but the pattern of play did not change when the contest resumed, with Spain looking the most likely to take the lead. The breakthrough came in the 59th minute, when Alonso's well-struck low effort from 25 yards was precise enough to beat Abdullah. A glut of substitutions followed, with Jesus Navas, Pedro Rodriguez, Carlos Marchena and Joan Capdevila coming on at the expense of Iniesta, Ramos, David Silva and Carles Puyol.
Saudi Arabia replaced goalscorer Hawsawi and Naif Hazazi with Yahia Al-Shehri and Al-Sahlawi as they sought an equaliser. And Al Nassr striker Al-Sahlawi did not take long to make an impact, picking the ball up from his fellow substitute and beating Casillas with a crisp low shot from 18 yards in the 74th minute.
The lively 23-year-old tried his luck again five minutes later after Alonso had fouled Al-Shehri, but his dead-ball effort went over the bar. Llorente, a 70th-minute replacement for Villa, shot wide from 18 yards in the 83rd minute as Spain pressed for the winner. They looked set to be frustrated by some determined Saudi defending, until Llorente headed in Alonso's corner.