The children and male-line grandchildren of The Sovereign are automatically HRH Prince/Princess of the UK by right of birth because they are in lineal succession to the throne. This is a courtesy style reflective of their royal rank and precedence, outranking hereditary Peers of the Realm who are titled, because they are closer to the throne in their own right.
Their wives, consistent with common law and practice in the Peerage, automatically enjoy the status, style and rank of their husbands, which means they automatically become HRH Princess of the UK upon marriage. If their husbands are created Peers, their wives then take their titles as the wife of a Peer (i.e. The Countess or Duchess of X) but with royal rank as HRH.
Males do not share their wives' rank, so in the case of Philip, he was granted his own title with royal rank by George VI before marrying The Queen. This was mainly done to ensure The Princess Elizabeth was marrying a man of equal rank who was not a commoner (i.e. HRH The Duke of Edinburgh). Once she was The Sovereign, she issued Letters Patent elevating Philip to the style and rank of a Prince of the UK in 1957.
The style and rank of "HRH Prince/Princess of the UK" is a courtesy title. Anyone who is not The Sovereign or a Peer is a commoner in the UK. They enjoy this style and rank as a personal honour, whereas a Peerage is hereditary and eventually loses royal rank when it passes to the great-grandchild of a Sovereign.