The Pope’s skullcap is actually called a zuchetto. It is not a yarmulke. It is a vestment proper to the clergy and is color-coded, as are all other clerical vestments. The Pope wears white, Cardinals red, Bishops violet, and priests black. Priests don’t commonly wear the zuchetto anymore, but all bishops do.
It came about as most other vestments have: at one time it had a practical use and then gradually became incorporated into liturgical vestments as a status symbol. The zuchetto was worn basically because Churches were big and cold (no heat back in the day) and the priests–who were generally older men–wore them to keep their heads warm. The same is true of episcopal Mitres.
For more, see
Also, fun fact–if you bring the Pope a new zuchetto, he’ll give you his old one. John Paul II did this, anyway. Maybe Benedict XVI does as well.