Hi,
In terms of purchasing second hand textbooks that are hardcopy's, it is legal even if you didn't pay for it. This is because the original purchase was given to the author/ publisher. This is why library's are allowed to allow the public to borrow books for free.
However, in terms of the pdf versions that are second hand, you have to be careful on this. The laws surrounding this is similar to pirate movies. I would say it would depend on how the book is accessed. When people purchase ebooks, they are often added to an account with a password. For example, on apple books, the copy of the ebook is stored on your apple id and cannot be downloaded as a pdf. This is legal however, if the ebook is a pirated version on a pdf that can be shared, this act is illegal as in this case, someone would have illegally scanned, downloaded or printed the textbook.