The segregation of baryons and dark matter during halo assembly