When it comes to Danzig (Gdańsk) there were two instances of the city being called 'Free City'. One was resulting from the military successes of Napoleon Bonaparte and lasted from 1807 to 1814, the other was the result of diplomatic agreements after the Great War and lasted from 1919 until 1939. Both, as well as Free City of Krakow, were autonomous countries and protected states.
Free City of Gdańsk (1807-1815)
The first Free City of Danzig has been proclaimed in Franco-Prussian Treaty of Tilsit in 1807 as an autonomous territory and a nominal protectorate of Prussia and Saxony, although in practice it was a French protectorate. Now, the whole territory of Poland has been gradually divided between Prussia, Austria and Russia during three cessions that happened in 1772, 1793 and 1795. Gdańsk, along with adjoining part of Pomerelia that was not claimed by Prussia during the First Partition was captured by Prussia in January 1793. In the course of Napoleonic campaign against the forces of the 4th Coalition, the French forces, supported by Poles and Saxons captured the city after two almost three months of siege. Having defeated Prusso-Russian forces, Napoleon created the Duchy of Warsaw and Free City of Gdańsk from the lands claimed previously by Prussia. This was compliant with the Napoleon's policy of establishing sovereign countries in the conquered lands, usually under French protectorate, such as Kingdom of Westphalia or Rhein Union (Rhine Confederation).
The Free City of Gdańsk was essentially a sovereign state with its own government. comprising of Senate (City Council) tasked with managing all the municipal offices and finances; The Bar, governing all the judicial matters and the Third Order, comprising of the representatives of all the districts and overseeing local laws. Senate was also connfering with the French Governor (for the most time, this office has been held by Jean Rapp) who nominally was responsible for all the military issues and overseeing French garrison stationing in the city, but was often interfering with other issues of the city. So, the Free City of Gdańsk was a Polish microstate, separate from another sovereign Polish state and French protectorate, the Duchy of Warsaw.
Free City of Danzig (1920-1939)
The other Free City of Danzig (Freistadt Danzig) was the result of diplomatic actions following the Great War. Similarly to its earlier counterpart, it was an autonomous city protected by the League of Nations. Danzig and especially the surrounding Pomerelia (part of Pomerania), although existing within the Prussian and later German borders for over 100 years, with the short period of partial independence courtesy of Napoleon, still had a substantial Polish minority that not only have built a strong national identity despite (or, as some would say, because of, intensive Germanisation and persecution in the last quarter of 19th and early 20th century). After the defeat of Germany, Entente countries wished to cripple its former enemy and thus planned to grant the whole Pomerania to reborn Poland what, in turn, caused a substantial turmoil in Germany. Given that both city and region was inhabited predominantly by Germans, authors of the Treaty of Versailles decided to separate Danzig from Germany but not grant it to Poland. Thus they created autonomous Free City of Danzig what was considered a bad idea by Germans and 'unfinished business' by Poles who were, however, content with the ability to freely use the Danzig maritime port (then the only Polish port).
Again, Free City of Danzig became an autonomous entity with own Constitution, bank, currency (transitory Danziger Mark until 1924 and Danziger Gulden afterwards), Parliament tasked with legislative issues and Senate that was the central executive institution. League of Nations had a constant presence and was significantly influencing the political and legal system of the city through the High Commissioner, nominated for the three-year tenure by the League and General Commissioner of Poland nominated by Polish Government. The latter was responsible for overseeing all the Polish institutions within the city and was also influencing city's foreign affairs. Additionally, although Danzig was located within the borders of Poland, Polish custom offices had no jurisdiction there, making the city a de facto free-trade zone (tax-free zone). After the 1933, when the NSDAP took power in Germany, the party gained also a strong influence in the Senate of Danzig and used it to suppress the freedoms of Poles and Jews living in the city. Following the German invasion of Poland, free city was dissolved by German Parliament and incorporated the territory of the Third Reich.
Free City of Krakow (1815-1846)
The Free City of Krakow was ins some way a relic of the Napoleonic Wars, as it has been created in 1815, after the dissolution of the Duchy of Warsaw at the Congress of Vienna. One of the decisions made by the Congress was to create an autonomous Polish state from the lands surrounding city of Krakow. Free City of Krakow, also called Krakow Republic was a partially democratic constitutional republic under the protection of Austria, Russia and Prussia. The constitution has been formalized during the Congress under the supervision of prince Czartoryski and lated developed by the commission consisting of the Krakow senators and representatives of Austria, Prussia and Russia. The constitution guaranteed equality, universal protection of private property and full religious tolerance. The country was ruled by the Senate holding executive power and Assembly of Representatives responsible for legislative matters. Members of both were elected from among the representatives of each of the constituent communes, members of the opposite institution (senators from among the representatives and vice versa), Cathedral Chapter (Catholic Church) and the University. The city had its own legal code (based on Napoleon's code), judicial procedures and police. It did not, however, had the ability to independently manage its foreign affairs, not it had separate currency, being in monetary union with Kingdom of Poland (this changed in the last years of the state existence).
In general, the Senate was dominated by more conservative nobility who supported the idea of merging Republic of Krakow with Kingdom of Poland (and had backing of Russia), while more liberal and republican Chamber of Representatives was in favour of expanding the independence of the Free City (they in turn were supported by Prussia). The political struggle between Senate and Assembly resulted in the petitions issued by leaders of these institutions to the Free City protectors what increased the degree to which the Russia, Austria began to intervene into the internal matters of the city. Prussia, profiting from the trade with Kraków remained neutral and even began to openly treat it like the sovereign state.
Constitution of the Free City of Krakow stated that the City could not collect customs but the neighbouring states also obliged to not impose customs on some commodities imported from the city. Low taxes, free trade in the neigbouring town of Podgórze (now a district of Kraków), abolition of serfdom and socage in favour of tenancy and rent and the development of infrastructure banking caused the population to double during the short existence of the Free City of Krakow with adequate increase of importance and wealth of the city. The support for the November Uprising that began in Kingdom of Poland in 1830 and increased interference by the Russia and Austria (Prussia was supporting the idea of independence but they did not want to risk antagonizing the two powerful neighbours). Attempt to create the uprising in the Free City of Krakow in 1833 caused the Austrian intervention followed by enforcing new, more restrictive constitution and further suppression of the liberties. This led to another uprising in 1846, after which Austria (with Russian approval) incorporated Free City of Krakow, ending its existence.