If the mother has hiv, how do you know the new baby doesn't already have hiv?

Blood work. Here are blood tests that can be performed that will confirm or deny the prescence od HIV in the baby.
You don't. You treat all initially as if they have it, starting anti-viral medicine and monitoring tests.There are some tests that suggest infection, but others that are ambivalent. It is usually after a year to 18 months before you can be sure (by tests) that the kid did not get infected.