The formation histories of galaxy clusters