Stem cells are totipotent and can give rise to any type of specialized cell in your body. Cell signaling induces horribly complex signaling pathways that lead to profound changes in transcription of that cell's DNA.
As an embryo, your gonads start in your chest and, through signals from your brain, end up in your pelvic region.
Erythrocytes (Red blood cells) are a great example of specialization. Before they become erythrocytes they have a nucleus. Their nucleus is ejected, their shape completely changes from spherical to flat-with-a-dimple and they are not a red blood cell. Obviously, erythrocytes cannot divide.