Leganes will play in La Liga for the first time in their history after winning promotion from the Segunda Division on the final day of the season.
Asier Garitano's side were within minutes of winning the league too, but Alaves' last-gasp equaliser against Gimnastic meant they pipped Leganes by a point.
Alaves had already sealed their spot in La Liga for next term, but would have dropped below Leganes by virtue of their rivals' superior head-to-head record had Manu Barreiro not levelled in the last minute at Nou Estadi de Tarragona.
Third-placed Gimnastic had to win and hope Leganes slipped up to seal second spot, but Pablo Insua struck shortly after half-time to give Garitano's men the lead and a place at Spanish football's top table.
Gimnastic, therefore, go into the play-offs along with Girona, Cordoba and Osasuna.