Spain's late winner knocked out Portugal and closed the book on Cristiano Ronaldo's time on the world's biggest stage.