The Rh factor is part of one's blood type; when we say someone has, for instance type A positive blood, the word positive indicates that they have the Rh antigen present on their red blood cells. In most cases, being Rh negative or positive is not important - however during pregnancy it can become a matter of life or death.
If a woman who has Rh negative blood (that is, her red cells do not have the Rh antigen on them) gets pregnant with a baby that has inherited the dominant Rh positive gene from the father, then her body can develop Rh antibodies - in effect, she develops an immunity to the Rh antigen. This will not affect the first pregnancy much, because the mother's and baby's bloods are prevented from mixing by the placenta, so the exposure between them does not occur until labor begins and the placenta tears.
However, if the same woman carries an Rh positive child during a subsequent pregnancy, her immune system will begin to attack the baby's red blood cells, causing severe anemia in the baby. The anti D shot, also known as RhoGam, was developed as a way to avoid this problem.
The Anti D shot is normally given to the mother during her pregnancy and after delivery; it is intended to remove Rh positive antigens from her body before her immune system can become sensitized to them. This shot is widely accepted as safe and effective when given as intended.
In your question you ask about giving the anti D shot to the baby. If the baby is Rh negative, the shot will be harmless; in the case of an rH negative baby being born of an Rh positive mother, it may actually do some good, although generally the risk of the baby having received antigens from the mother is small. However is the baby is Rh positive, the anti D shot will attack the baby's red blood cells, causing the same sort of anemia that it was invented to avoid.