Think back to science class if you’ve had one. When you are trying to combine one substance with another, there are certain factors you can control. Agitation, temperature, and time all influence dissolving. In the case of laundry you are trying to “undissolve” the stains and dirt away. More specifically you use a detergent which goes in there, finds the dirt particles, stick to it, then bonds to the water. Hot water makes it easier for the particles that comprise the stain to get loose and thus go away.
The problem is that dye is also particles, and when you increase the heat, you increase the chances of the particles loosening from the material. Then the detergent and water come in and move it all around. The dye really does love to stick to materials though, so it will adhere itself to any other material it can. This is why you get pink shirts when putting a red item into a load of white.
White clothing of course has no dye. This means you can apply a lot of heat to get out really rough stains and the cloth will be perfectly fine. Undergarments are often white not only because it’s less effort to not dye the item, but because you can get the really worn in stains and sweat out without risking any damage to the item.
Colored items should be washed in cold water to reduce any chances of having the dye run.
Black clothes are sometimes separated out because that black dye can also run, and that will taint all the other colors by shading them darker.
Nowadays detergents do a lot of the heavy lifting when it comes to cleaning our clothes, so it is OK to wash your whites with your colors provided you keep it cold. And since you use cold water, the chances of dye particles becoming loose is reduced, so if you’re lazy like I am you can put all your laundry together and have no problems with color transfer. That said, my whites could be whiter. But I don’t really care enough to go through the extra effort.