In short it is based on the land Charlemagne's dad, Pepin the Short, donated to the Pope after he conquered (northern) Italy.
For a little more long-winded explanation:
From the 4th century the Pope received land in donations/endowments. Mainly in and around Rome, but also in other parts of Italy. This land became known as the Patrimony of St. Peter. Gradually the land furthest away from Rome came outside of their control but in the area around Rome Papal power grew, and became more independant from the Empire.
In 754, Pepin the Short gave the Papacy the Exarchate of Ravenna and the Pentapolis (Rimini, Ancona, Fano, Pesaro, and Senigallia) in exchange for help against the Lombards. For a long time the Papacy struggled to exercice temporal powers in these areas.
In 774, his son, Charlemagne, confirmed the donation of his father; moreover, to give the papal claim to temporal power greater antiquity, the so-called Donation of Constantine was forged. It purported to be a grant by Roman Emperor Constantine I of great temporal power in Italy and the West to the papacy. On its basis later popes also claimed suzerainty over Naples, Sicily, and Sardinia.
In 1115, Countess Matilda of Tuscany, left her territories to the church. In the 13th and 14th century the Emperors renounced their claims to the duchy of Spoleto, the Romagna, and the March of Ancona, while the various free communes and smaller holdings (tyrannies) long resisted Papal suzerainity and actual control by its territories began only in the 16th century, when Cesare Borgia, son of Pope Alexander VI, conquered the petty states of the Romagna and Marche. After his fall in 1503 most of them passed directly under papal rule. In the early 16th century, Pope Julius II consolidated papal power by abolishing local autonomies and by participating effectively in the Italian Wars. The last principalities to lose their autonomy to the popes were Ferrara (1598) and Urbino (1631), while the duchy of Castro was added in 1649.