The institution, the third oldest university in the English-speaking world, was named the best in the UK and Scotland by The Times and Sunday Times Good University Guide.
The university, which was Scotland’s first when it was founded in the 15th century, is where former students Prince William and Kate, now the Princess of Wales, met while both were studying there in the early 2000s.
Published annually, The Times and Sunday Times Good University Guide provides a comprehensive guide to higher education in the UK.
The University of Oxford fell from first to second place in this year’s rankings, with the University of Cambridge retaining third place.
University College London won the guide's University of the Year 2024 award having moved up to sixth place in the league table this year.
The 2024 guide said St Andrews won due to improved results in graduate prospects and its student-to-staff ratio, at 11.9 to one.
The university's high proportion of students gaining first and 2:1 degrees, as well as high Ucas entry standards, also helped it top the league table.
Its Principal and Vice Chancellor, Prof Dame Sally Mapstone, said: "While positive external validation is always welcome, no vice chancellor ever sets out to top a league table, and what matters most to me are the individual and collective experiences of those who study and work at this great university.
"St Andrews never stands still; we are always looking for ways to improve what we do.
"I think everyone who works and studies here will be delighted to see that culture reflected in where St Andrews now sits as a sector leader."
The guide said its performance has improved in many measures in the past year – including graduate prospects and honours degrees awarded.
The guide evaluates everything from satisfaction with teaching quality and the student experience through to degree completion rates and graduate employment prospects.
Top 10 UK universities
1. University of St Andrews
2. University of Oxford
3. University of Cambridge
4. London School of Economics and Political Science
5. Imperial College London
6. University College London
7. Durham University
8. University of Bath
9. University of Warwick
10. Loughborough University
Helen Davies, editor of The Times and The Sunday Times Good University Guide, said: "The higher education landscape has never been tougher.
"It is more competitive to get a place at many of our top institutions; the cost of attending university has soared, leaving graduates with extraordinary debt; and in many cases campus life still bears the scars of the pandemic.
“Meanwhile lecturers are on strike and the marking crisis is a running scandal.
"It means any prospective student, parent or carer needs to think hard about whether university is the right choice, and then where to study and what subject."