The book I'm talking about is 'Secrets of the Darkest Arts' (which is the only book covering the subject of horcruxes to my knowledge). Riddle presumably read this book prior to him asking Slughorn about them as he already knew what they were (he just wanted information on how to make multiple horcruxes). Anyway, why did they keep the book in the library (although in the restricted section) if it was a banned subject (Slughorn told Riddle the subject was banned when asked about them). Basically any student could have found the book and learned about them.
According to Slughorn there may not have been much about Horcruxes in the library to begin with.
‘No ... well ... you’d be hard-pushed to find a book at Hogwarts that’ll give you details on Horcruxes, Tom. That’s very Dark stuff, very Dark indeed,’ said Slughorn. - -Half Blood Prince
The books that did mention Horcruxes were removed from the library, we are just unsure when it occurred exactly.
‘And once we get hold of it, how do you destroy a Horcrux?’ asked Ron.
‘Well,’ said Hermione, ‘I’ve been researching that.’
‘How?’ asked Harry. ‘I didn’t think there were any books on Horcruxes in the library?’
‘There weren’t,’ said Hermione, who had turned pink. ‘Dumbledore removed them all, but he – he didn’t destroy them.’ -Deathly Hallows
And again later in the same chapter Hermione states this, showing Slughorn may have been mistaken.
‘This is the one that gives explicit instructions on how to make a Horcrux. Secrets of the Darkest Art – it’s a horrible book, really awful, full of evil magic. I wonder when Dumbledore removed it from the library ... if he didn’t do it until he was Headmaster, I bet Voldemort got all the instruction he needed from here.’
We can probably safely assume that when Dumbledore was made Headmaster he would have removed them for sure, but I believe that the fact that Slughorn mentions how strongly Dumbledore was against Horcruxes when speaking to Riddle he may have already removed the books.
It’s a banned subject at Hogwarts, you know ... Dumbledore’s particularly fierce about it ...’ - Half Blood Prince
Three points to note.
We don't know when this ban went into effect in the 1000 years of Hogwarts' existence, before Slughorn's conversation. Dumbledore himself may have been the root cause of the ban.
Being a banned subject may only mean it's not taught in school, while owning a book that contains details about it in the restricted section of the library for study by advanced students may be, or was, allowed.
Finally, Hogwarts is not the only location in which books can be found, and Voldemort likes to cover his trail and keep up his good boy persona (in school), so finding books on Horcruxes outside of school would have appealed to him as well.
Books in the restricted section are, as we see in Chamber of Secrets, not usually available to students. They need express permission in writing from a teacher for every book they want to access:
‘Er – Professor Lockhart?’ Hermione stammered. ‘I wanted to – to get this book out of the library. Just for background reading.’ She held out the piece of paper, her hand shaking slightly. ‘But the thing is, it’s in the Restricted Section of the library, so I need a teacher to sign for it – (…)’
Madam Pince held the note up to the light, as though determined to detect a forgery, but it passed the test. She stalked away between the lofty shelves and returned several minutes later carrying a large and mouldy-looking book.
(Harry Potter and the Chamber of Secrets, chapter The rogue Bludger)
Apparently, Amando Dippet and his predecessors believed that to be sufficiently safe. It wasn’t before Dumbledore had become Headmaster (after Riddle had asked Slughorn about Horcruxes) that the books about Horcruxes were removed from the library altogether.
Others may not be as ‘fierce’ as Dumbledore.
Horcruxes were considered a banned subject at Hogwarts during Voldemort’s time as a student, which may have been in no small part due to Dumbledore’s influence there.
“But all the same, Tom … keep it quiet, what I’ve told – that’s to say, what we’ve discussed. People wouldn’t like to think we’ve been chatting about Horcruxes. It’s a banned subject at Hogwarts, you know … Dumbledore’s particularly fierce about it …”
- Harry Potter and the Half-Blood Prince, Chapter 23 (Horcruxes)
Previous headmasters may not have felt as strongly as Dumbledore, and left the books for educational purposes. The degree to which subjects are banned likely depends on the headmaster’s judgement - when Dumbledore became headmaster, he removed the books referencing Horcruxes entirely. The previous headmasters may have simply not seen the need for this specific measure.