The purpose of the Rhogam shot, given after delivery of a Rh+ baby to a Rh- mother, is so the mother will not develop antibioties that would sensitize her to future Rh+ babies and complicate those pregnancies. The reason this injection is needed only right after delivery is because the only time during the whole pregnancy that the mother's blood and the baby's blood can come into contact is during delivery.
It is essential for an Rh- mother to have the Rhogam injection after a known miscarriage, too. Usually you cannot know if the baby was Rh+, but it's important to get the Rhogam anyway, just in case.
If Rhogam is not given in the first 24 hours after delivery, the mother's immune system will begin producing antibioties to Rh+ after the Rh+ baby is born. If she conceives Rh+ babies in the future, her body will perceive that Rh+ baby as an intruder (like an infection), and will try to protect the mother by ridding the body of the intruder. This situation requires careful monitering of the 2nd and following babies by the doctor and sometimes makes an early delivery necessary. The 2nd Rh+ pregnancy is usually affected late in the pregnancy, but each Rh+ pregnancy thereafter is affected earlier as antibioties are increased at each exposure.
Rhogam is a wonderful blessing and heads off a world of problems and worry.
Hope this helps! Barbara