The Sharingan sees chakra as color, and Genjutsu works off chakra, so it stands to reason that the built in Genjutsu detection is actually chakra detection. If the Genjutsu caster didn't personally add it, an illusion of a person would to a Sharingan look like a hollow puppet. It would be lacking the chakra network all living people must posses to live (recall chakra exhaustion results in death, which is shown twice during Pain's invasion), thus obviously be Genjutsu.
Though, as Islam Elshobokshy says, it does not render you immune to Genjutsu. It only makes it easier to Detect Genjutsu. As Sasuke says in one of the dubs, "My eyes can see through Genjutsu". In Sasuke vs Itachi, we see both of them play along to each others Genjutsu, but also clearly show they knew it was all an illusion in the beginning of the fight. Zetsu commented on how they just stood around not moving. The Tsukuyomi however seemed to fool Sasuke, until he broke through it.