How long does a book need to sit on a self, not getting checked out, before the library gets rid of it?
It depends upon the library. I volunteer at one in a small space in the city. We have a lot of space issues, so our current policy is that anything that hasn't been checked out in 18 months gets pulled from the shelves. The exceptions are those we believe will be of special interest to our patrons. However, I used to volunteer at a library in a more rural area. We had plenty of space and not enough books to fill the space. We only discarded those that were in bad shape.
I don't think it is that much of a popularity contest. Some books should never be thrown out. In practice they are, on the basis you describe but the books that replace them are less worthy.
Different policies in different libraries, mainly dependent on how much space they have available.
At my library it is 3 years