Paulinho's late header fired Brazil into the final of the Confederations Cup with a hard-fought 2-1 win over Uruguay.