As long as the page takes to be ready, and no longer. A preloader should leave when the page has loaded, not after a fixed number of seconds.
Why a fixed timer fails
Set a preloader to three seconds and it is wrong for nearly everyone. On a fast connection the page was ready after one second, and the other two are stolen from the visitor. On a slow one the preloader ends on time and shows a page that is still assembling, which is the thing it was there to hide.
What a good length feels like
Short enough that nobody thinks about it. A preloader that is enjoyed at one or two seconds is resented at five. If yours regularly runs long, the preloader is not the problem; the page is slow, and that is where the work is.
Setting the pace
The length should follow the page, but the pace of the animation is a choice. Segue’s preloaders hold while the shop loads, leave as soon as it is ready, and have a Speed slider from 50 to 150 percent: every move keeps its shape and only its clock changes.