It's a personal choice thing, as the others have said. My personal attitude is ......
Top priority is reliability, I don't want the hassle (cost time etc) of having to have stuff fixed (or fixing it myself).
Safety generally comes with age of car, the younger the better.
Rust comes with age, the younger the better (broadly). Don't buy from near the coast.
Check the MOT history of any car you're interested in, plus check similar cars to see what if any common faults there are (the vast majority of fails are lights, tyres, wipers, all non-issues, the ones you don't want to see are suspension failures, rust, systematic brake issues, emission failures)
If I want to keep it for maybe 5yrs, then what is it going to be like at the end of that? Buying younger often gets you more expected remaining trouble free life.
As others have alluded to, general condition is usually fairly evident, has it been looked after? Smell the car, it should NOT smell damp or musty. Water leaks are a pain and will cause a lot of damage, walk away.
I tend to think 10yrs old/50k miles is a bit of a turning point. Up to that most cars usually give little trouble, after that it all depends on how it's been treated. You'll always find someone who has a 20yr old 150k miler which has been 100% relaible and has no rust, but you're lowering the risks by going younger. I recently swapped a 20yr old Yaris for a 10yr old one, and the 10yr old is a far better car in many respects (safety, features etc), though there is more to go wrong.
My Jazz is 2018 and low low miles. Fingers crossed.