Alonso began his career at Real Sociedad, the main team of his home province Gipuzkoa. After a brief loan period at Eibar, he was appointed as team captain of Real Sociedad by then-manager John Toshack. He succeeded in the role, taking the club to second place in the 2002–03 season. He moved to Liverpool in August 2004 for £10.5 million and won the UEFA Champions League in his first season, under manager Rafael Benítez, scoring the equalising goal in the Final against Milan.
The following season, he won the FA Cup and the FA Community Shield. He moved to Real Madrid for the start of the 2009–10 season in a deal worth around £30 million. After winning honours including a league title in 2012 and the Champions League in 2014 during five seasons in Madrid, he was signed by German club Bayern Munich on a two-year contract. This was extended by a further year, and he eventually retired from playing in summer 2017, aged 35, having won the Bundesliga in each of his three seasons with Bayern. He made his international debut for Spain in April 2003 in a 4–0 victory against Ecuador.
While playing for Spain, Alonso won Euro 2008, Euro 2012 and the 2010 World Cup, and he also represented his country at Euro 2004 and the 2006 World Cup. On 23 June 2012, Alonso won his 100th cap for Spain in the quarter-final of Euro 2012 against France; he celebrated the occasion by scoring both of Spain's goals to send them into the semi-finals. Following Spain's failure to progress out of the group stages at the 2014 World Cup, Alonso retired from international football on 27 August 2014. His 114 caps make him the seventh-most capped player in the nation's history.