In most countries, a publisher has to send companies of newly printed books to repositories, or often called deposit libraries or national libraries. There have been many famous libraries through the ages, though the old libraries seemed to be more like our present day deposit, as the books were chained to their stands, and could not be borrowed.
In year 2000 UNESCO made recommendation for the construction of legal deposit legislation, though the books have been deposited in many national libraries as far back as the 1500s. In the UK the most exciting ones to visit are (in my opinion) the British Library and the Bodlean. Each country has its own regulations, and the number of books deposited is usually between one and six though some countries, like Poland, require as many as 19. Authors do not receive royalties on these books, and publisher are not paid.
The books have to be sent in within a year, sometimes as soon as within three months.
As of the 6th of April 2013, all digital media in the UK has to be sent to deposit libraries. In Sweden this became a law already as of 1.1. 2012.
The beautiful Bodleian Library in Oxford