There were a number of different ways that colored people got freedom in the slave south. Increasing numbers of free people of color were a destabilizing influence on the slave system, which is why many southern states passed laws forbidding manumission and mandating enslavement of free colored people.
Some had always been free. The first Africans arrived in Virginia before the full development of the color-based slave system. cf. Anthony S. Parent Jr. Foul Means/The Formation of a Slave Society in Virginia, 1660-1740. Some of these men were released after serving a period of indenture, never having been fully enslaved.
Some colored people arrived free in the North American colonies from places where their freedom was more usual, such as the West Indies, parts of South America, or Africa itself.
Since there were few African men and even fewer African women in North America at this early stage, some of these black founders married Indian or white women. The fact that Virginia passed laws against white women marrying colored men proves that it happened often enough to pass a law against it. cf. Catherine Clinton and Michele Gillespie The Devil's Lane/Sex and Race in the Early South.
These families and others gave rise to free colored populations in Virginia and elsewhere who were increasingly socially isolated by the caste system that progressively developed more or less throughout the American colonies. cf. Tim Harshaw, Children of Perdition/Melungeons and the Struggle of Mixed America. In early Virginia, a person with only one colored great-grandparent, an 'octaroon', was considered legally white for purposes of civil rights. Later, the infamous 'one drop of blood' rule was applied to disenfranchise victims.
Some slaves were freed because they had become too old to work productively, so that their owners no longer had to feed and clothe and house them. They were left to the charity of their children (still enslaved) or of the parish, or of no one in particular.
Some slaves were freed for good service, or for military service, or because their owner thought owning slaves was wrong.
Some slaves were freed because they were the children of their white masters. As a modern, I found it difficult to grasp that it was common for one part of a wealthy southern family to own another part. cf. Annette Gordon-Reid, The Hemingses of Monticello for a study of one particularly well known multi-racial Virginia family.
What did free colored people do when the race laws were strengthened, sometime to the point of enslavement? Many left, moved north or west or to some place like Louisiana where the laws were not yet so oppressive. Some fell victim and were enslaved.
Some claimed Indian ancestry and treaty rights. One of the threats that was used to push the Civilized Tribes west in the 1830s was that if they stayed in their homes, they would be subjected to the laws of the southern states as colored non-citizens without civil rights. Some stayed anyway, keeping a precarious freedom under extremely oppressive and dangerous conditions.
Some went underground, passing illegally for white if their appearance allowed. Some claimed to have dark-complexioned ancestors who were not considered African – Portuguese or Turkish. It was critical to these families that they maintain good relations with white neighbors and be socially accepted as white, in order to maintain their legal status and civil rights. Men of these families would have been eager to enlist in the local militia company, in order to maintain their right as whites to bear arms – some of them may have been the dark faces sometimes seen in uniform in the old photos of Confederate volunteers.
American slavery was a cruel system, one of the worst in the history of the world. It has been over now for 150 years, but some of the harm it did still remains. Now go study.