There are only a certain number of students that medical schools can take. This is limited by the government, but also by the ability of the medical school to train students. When I am on placement, there are around 30 of us in one specialty at any given time. We have to share a set number of consultants and attend their ward rounds and clinics. There is only a set number of students that can be accommodated in this system. There are also only a certain number of foundation jobs, which are not being recruited for from abroad.
From my experience at medical school, there do seem to be a relatively higher number of students from a more privileged background (having gone to grammar/private schools etc), but this isn't necessarily due to the medical schools themselves, especially as they don't get told what school you are applying from. There is also a great push from a lot of medical schools to support people from widening participation backgrounds to attend, as they might not get the support from their school or community to do so.
At the end of the day, it is a notoriously competitive subject. There are many people who get the grades who would do very well, and also those who don't get AAA who could succeed and become great doctors.