It is mostly based on the barring gene, which is on the Z sex chromosome.
Females have ZW, so they can only have one barring gene.
Males have ZZ, so they can have two barring genes, and that makes them lighter than the females that have one.
Most autosexing breeds are based on wildtype (e+) coloring, like Cream Legbars and Bielefelders. Two barring genes makes the males have a head spot, and overall lighter color when they hatch. There must be some other genes that also affect it, because such breeds can be selectively bred to show the differences more clearly, or the sexing trait can be ignored and end up with ones that do not show the differences very clearly.
Barred Rocks and other black-based barred breeds can also be autosexing, with some selection and a bit of practice. The males typically hatch with a larger head spot, and lighter legs. They grow feathers with more white. The females have darker legs, their head spot is generally smaller and more clearly defined, and their feathers are overall darker because they have less white.