>>12613315essentially what
>>12613329 said
but in reply to you here
>>12613354I went to a top 10 (global) university. We had large lectures, but small classes (of about 8 people) so you could get to know professor pretty well; at least in my case where i'd meet with the same professors once every week or two weeks for a year or longer.
Similarly, if you volunteer in a lab, or do your final year thesis in contact with a professor or post doc, then you can build up a good relationship.
Then if you are a reasonably nice person to be around and work with, and are hardworking and intelligent, then these contacts will gladly provide reference letters, and what not. In fact, I spoke with a professor in december who wrote me a reference letter for my masters in 2018. They said to me, if i need advice to contact them; but they aren't in the same area of research that im interested in, and i hadn't spoken to them before that since december 2018.
I imagine its more difficult if you don't have time to develop a relationship with professors, or if your potential referees think you aren't hardworking or intelligent, or if they think you aren't enjoyable to work with.
For example, look at this letter that John Nash had. Can you imagine a professor writing about you like that?