How exactly does Spinoza ground the legitimacy of the government in the theologo-political treatise? More specifically in Chapter 16, what is the legitimacy of the government grounded in?
What grounds the legitimacy of any government
Jonathan Israel provides the basic answer.
It is greatly to men's advantage, stresses Spinoza, 'to live in accordance with the laws and sure dictates of our reason' and 'in safety from fear as far as possible'. Incontestably, to 'achieve a secure and good life, men had to unite in one body' and form a commonwealth. Nevertheless ... Spinoza continually adduces the 'state of nature' as a measuring-rod for assessing political and moral phenomena, and identifying what is best and most essential in human existence. (Jonathan I. Israel, Radical Enlightenment : Philosophy and the Making of Modernity 1650-1750, Oxford : OUP, 2001: 271.)
Democracy as the best regime
The connexion with democracy is immediate. Israel continues :
Hence democracy is declared better than monarchy and aristocracy, because it is the 'most natural form of state, approaching most closely to that form of freedom which Nature grants to every man'. Transferring one's individual sovereign right (power) to act, as it was in the state of nature, to the majority rather than to one or a few, means no one is set above others : 'in this way all men remain equal, as they were before in the state of Nature'. (Israel: 271.)
Steven B. Smith supplements this the following commentary:
Among students of Spinoza it is generally acknowledged that chapter 16 represents a new beginning in the Theologico-Political Treatise. ... [A] century before Rousseau, Spinoza embraced democracy as the form of government most consistent with the natural law and in agreement with human liberty. ... Spinoza's support of democracy is twofold: first as the regime most consistent with the modern "scientific" interpretation of natural law and justice and second as the regime most likely to foster the intellectual and rational capacities of its citizens. At the same time that Spinoza advocated democracy as the most rational regime ... he was keenly aware of the limitations of democratic rule. While he regarded democracy as the best regime (optima Republica), he did not regard it as the highest good (ens perfectissimum) for the individual. In the end there will always be a disproportion between the democratic citizen and the intensely personal, even solitary, character of the philosophical life. (Steven B. Smith, 'Spinoza's Democratic Turn: Chapter 16 of the "Theologico-Political Treatise"', The Review of Metaphysics, Vol. 48, No. 2 (Dec., 1994), pp. 359-388: 359-60.)
[Spinoza turns] to the construction of the best regime in the last quarter of the work. This regime would be neither the virtuous republic of classical antiquity nor the holy city of the Bible but the commercial metropolis of modernity. The optima Republica of the Treatise is, above all, a democracy. Spinoza's defense of democracy needs to be distinguished from two widely held views in the history of political theory. Contrary to the first view, ironically, the first avowed defender of democracy did not place any great confidence in the wisdom, either actual or potential, of the people as a whole. The multitude is and will remain prone to superstition and credulity so the purpose of politics is to find a means, both institutional and psychological, of restraining the passions. Because rulers and ruled alike are subject to the same passions, democracy is the regime most likely to subordinate the interests of both subjects and rulers to the interests of the whole.
Contrary to the second view, upheld by Hobbes, Bodin, and the politiques, Spinoza did not regard the achievement of peace and the avoidance of conflict as the chief goals of political life. As important as these ends are, the Treatise held out a more positive role for politics than the avoidance of the summum malum. Spinoza endorses the democratic republic because it is the regime most consistent with the autonomous individual or liberated self. Democracy is desirable because it fosters the conditions for reason and the expression of our individual faculties. This democracy bears an uncanny resemblance to seventeenth-century Amsterdam. One can say, with perhaps only slight exaggeration that Amsterdam is for Spinoza the new Jerusalem, a commercial republic based upon freedom of trade, freedom of religion, and freedom of opinion. Anticipating the title of Gershom Scholem's autobiography, he could almost have subtitled the Treatise "From Jerusalem to Amsterdam." (Smith: 362-3.)