Black codes were laws enacted by states and towns of the United States in the 1800s to restrict the rights of free blacks. While these laws became popular in Southern States in the interim between the end of the Civil War and the start of Reconstruction, many such laws also existed in Northern ("free") states decades earlier.
Black codes should not be confused with Jim Crow laws which less blatantly prescribed discrimination by requiring separate public facilities for blacks and whites. Black codes specifically set forth onerous burdens on blacks in the states in which they had been enacted, nominally with the intent to deter "free" blacks from entering the state (in the Northern cases), or to prevent them from being able to abandon their previous slave-labor roles (in the Southern cases).
State laws banning the marriage of whites and blacks, so-called anti-miscegenation laws, existed in all the slave states and in several new free states as well. Many of these remained in effect until they were declared unconstitutional in 1967. Black codes incorporated into the state constitutions of Indiana and Illinois in the 1840s completely prohibited black immigration into those states.
After the Civil War, many Confederate states passed codes that singled out free blacks for sharp penalties for skipping work or for being unemployed, and stiff fines for otherwise acceptable public acts were enacted specifically towards or involving blacks.
These Black codes by Southern states after the Civil War garnered hostility in the North, who accused the South of trying to perpetuate the illegalized practice of slavery through onerous regulations. The dominance of Northern carpetbaggers in Southern politics brought many Southern black codes to an end in 1866.