Many libraries keep several copies of major course textbooks available. They can't however maintain potentially hundreds of copies of each book, so demand swamps them pretty fast.
Because the students judge the utility of having a textbook available as worth the price (or in some cases the text books come with a code that you have to have to pass the class). Same reason a book collector may prefer buying a book over checking it out at the library.
It is like asking if cooking for for yourself is so cheap and healthy, why do people eat out so much.