There were two main reasons why the Southern states did this. First, they wanted to keep the idea of white supremacy. They wanted to make perfectly clear which race was in control even though slavery had ended. The second reason was an economic reason. The white elites needed black labor because that was the basis of their entire economy. They enacted the black codes to ensure that the former slaves would have few options other than to work for the white landowners. In these ways, the black codes were meant to preserve the basic relations between the races that had existed before the Civil War.
Black Codes were passed by Southern state legislatures. They were intended to place limits on the socioeconomic opportunities and freedoms open to Black people. What it did was that it forced Black Americans to work under conditions that closely resembled slavery.