The mucus plug acts as a protective barrier for the baby, preventing bacteria from entering the cervix.