Specifically in the 16th and 17th centuries. Were these cities expected to provide anything to the empire? To what extent would the Emperor protect them? What prevented them from being absorbed by nearby principalities?
Ah yes, ye old central european clusterfuck of complexity. The HRE. Let's make it a little more complex by ignoring the period from the mid 13th century in which Imperial immediacies were first created and jump into the 16th century. Actually scratch that, you'll have to read through a brief history of Imperial Immediacies before we get to 16th & 17th century free cities or it'll seem like the Emperor just had an aneurysm when he first granted cities this status.
So a Free Imperial City is one of several forms of Imperial Immediacies that existed in the HRE, all it basically means is "under direct authority of the emperor". This status could be extended to a person (such as an Imperial Knight), a city or an entire pricipality.
Originally this meant that the city or principality was considered under direct imperial rule, something that had advantages (in that your local lord didn't, well, lord over you) but also disadvantages in that the Emperor did instead. Having the Imperial Court set up in your city could be a costly thing (keep in mind that the Imperial court originally had no permanent seat of residence), as could being pulled into a war along with the Emperor.
However, by the 16th century the power of the Imperial throne had waned considerably and the status as Free Imperial City come to mean that rather than be under the direct rule of the Emperor, the City had the right to exercise certain rights usually reserved for the emperor or local lord. Examples of this include things like declaring war, minting your own currency, collecting taxes and tolls and holding your own legal proceedings. Basically, it was a form of limited local autonomy. Originally created to be under direct Imperial authority they came to gradually possess privileges originally reserved for Emperor and Electors due to the waning power of the imperial throne.
Sources:
Das Alte Reich 1648–1806 - Karl Otmar Freiherr von Aretin
Germany and the Holy Roman Empire - Joachim Whaley