One common formulation is that the purpose of the state is to protect rights and to preserve justice. A countervailing formulation is that government exists to protect privileges of a few, preserving a state of injustice for the majority. But this raises more questions than it answers. Which and whose rights? What sort of justice? There are, after all, many different conceptions of what rights are, and what justice consists of.
It is on those questions that one can find the differences between conservatism, liberalism, libertarianism, socialism, fascism and other political philosophies. There are a handful of anarchists (see anarchism) among the socialists (see traditional anarchism) and among the libertarians (see anarcho-capitalism), but everyone else agrees that the existence of some kind of government is morally justified. What they disagree about is what government should do.
One fairly useful way to conceive of the differences between these different views is as how much they want government to do. For a stark and timely contrast, consider two of these views: libertarianism, which wants the state to do only a few things, and socialism (except for anarchism), which wants the state to do a lot of things (but only as a transition to communism by some definitions of socialism). Libertarianism, in political theory, is the view that the function of the state is only to keep people from harming each other. In other words, individuals should be free to do anything they want, so long as they do not infringe upon the equal rights of others to do what they want. The government's role is to protect those rights. Socialism, nearly on the other end of a continuum, is the view that the state is responsible for an equitable distribution of wealth and for controlling the means of production and distribution of resources in an economy.
The constitutions of various countries codify practical views as to the purposes of their governments, but they tend to do so in rather vague terms, which particular laws, courts, and actions of politicians subsequently flesh out. For example, the Preamble of the United States Constitution lists the items states that the purpose of the Constitution--which defines the American state--is "to form a more perfect union, establish justice, ensure domestic tranquility, provide for the common defense, promote the general welfare, and secure the blessings of liberty to ourselves and our posterity." The phrase "promote the general welfare" has, since the 1930s, been used to defend the proposition that the United States should impose a social "safety net", or welfare system; others, however, have disagreed that the phrase can be properly interpreted that way, pointing to the use of "promote" as opposed to "provide". But it is in this sort of way that various countries have translated vague talk about the purposes of their governments into particular state laws, bureaucracies, enforcement actions, etc.