Fundamental theorems of welfare economics
There are two fundamental theorems of welfare economics. The first states that in economic equilibrium, a set of complete markets, with complete information, and in perfect competition, will be Pareto optimal (in the sense that no further exchange would make one person better off without making another worse off). The requirements for perfect competition are these: 
 There are no externalities and each actor has perfect information. Firms and consumers take prices as given (no economic actor or group of actors has market power). The theorem is sometimes seen as an analytical confirmation of Adam Smith's "invisible hand" principle, namely that competitive markets ensure an efficient allocation of resources. However, there is no guarantee that the Pareto optimal market outcome is socially desirable, as there are many possible Pareto efficient allocations of resources differing in their desirability (e.g. one person may own everything and everyone else nothing). The second theorem states that any Pareto optimum can be supported as a competitive equilibrium for some initial set of endowments. The implication is that any desired Pareto optimal outcome can be supported; Pareto efficiency can be achieved with any redistribution of initial wealth. However, attempts to correct the distribution may introduce distortions, and so full optimality may not be attainable with redistribution. The theorems can be visualized graphically for a simple pure exchange economy by means of the Edgeworth box diagram. History of the fundamental theorems

Adam Smith (1776) 
In a discussion of import tariffs Adam Smith wrote that:
Every individual necessarily labours to render the annual revenue of the society as great as he can... He is in this, as in many other ways, led by an invisible hand to promote an end which was no part of his intention... By pursuing his own interest he frequently promotes that of the society more effectually than when he really intends to promote it.Note that Smith's ideas were not directed towards welfare economics specifically, as this field of economics had not been created at the time. However, his arguments have been credited towards the creation of the branch as well as the fundamental theories of welfare economics. Léon Walras (1870) 
Walras wrote that 'exchange under free competition is an operation by which all parties obtain the maximum satisfaction subject to buying and selling at a uniform price'. F. Y. Edgeworth (1881) 
Edgeworth took a step towards the first fundamental theorem in his 'Mathematical Psychics', looking at a pure exchange economy with no production. He included imperfect competition in his analysis. His definition of equilibrium is almost the same as Pareto's later definition of optimality: it is a point such that...
in whatever direction we take an infinitely small step, P and Π [the utilities of buyer and seller] do not increase together, but that, while one increases, the other decreases. Instead of concluding that equilibrium was Pareto optimal, Edgeworth concluded that the equilibrium maximizes the sum of utilities of the parties, which is a special case of Pareto efficiency:
It seems to follow on general dynamical principles applied to this special case that equilibrium is attained when the total pleasure-energy of the contractors is a maximum relative, or subject, to conditions...

Vilfredo Pareto (1906/9) 
Pareto stated the first fundamental theorem in his Manuale (1906) and with more rigour in its French revision (Manuel, 1909). He was the first to claim optimality under his own criterion or to support the claim by convincing arguments. He defines equilibrium more abstractly than Edgeworth as a state which would maintain itself indefinitely in the absence of external pressures and shows that in an exchange economy it is the point at which a common tangent to the parties' indifference curves passes through the endowment. His definition of optimality is given in Chap. VI:
We will say that the members of a collectivity enjoy a maximum of ophelimity [i.e. of utility] at a certain position when it is impossible to move a small step away such that the ophelimity enjoyed by each individual in the collectivity increases, or such that it diminishes. [He has previously defined an increase in individual ophelimity as a move onto a higher indifference curve.] That is to say that any small step is bound to increase the ophelimity of some individuals while diminishing that of others. The following paragraph gives us a theorem:
For phenomena of type I [i.e. perfect competition], when equilibrium takes place at a point of tangency of indifference curves, the members of the collectivity enjoy a maximum of ophelimity. He adds that 'a rigorous proof cannot be given without the help of mathematics' and refers to his Appendix. Wicksell, referring to his definition of optimality, commented:
With such a definition it is almost self-evident that this so-called maximum obtains under free competition, because if, after an exchange is effected, it were possible by means of a further series of direct or indirect exchanges to produce an additional satisfaction of needs for the participators, then to that extent such a continued exchange would doubtless have taken place, and the original position could not be one of final equilibrium. Pareto didn't find it so straightforward. He gives a diagrammatic argument in his text, applying solely to exchange, and a 32-page mathematical argument in the Appendix which Samuelson found 'not easy to follow'. Pareto was hampered by not having a concept of the production–possibility frontier, whose development was due partly to his collaborator Enrico Barone. His own 'indifference curves for obstacles' seem to have been a false path. Shortly after stating the first fundamental theorem, Pareto asks a question about distribution:
Consider a collectivist society which seeks to maximise the ophelimity of its members. The problem divides into two parts. Firstly we have a problem of distribution: how should the goods within a society be shared between its members? And secondly, how should production be organised so that, when goods are so distributed, the members of society obtain the maximum ophelimity? His answer is an informal precursor of the second theorem:
Having distributed goods according to the answer to the first problem, the state should allow the members of the collectivity to operate a second distribution, or operate it itself, in either case making sure that it is performed in conformity with the workings of free competition. Enrico Barone (1908) 
Barone, an associate of Pareto, proved an optimality property of perfect competition, namely that – assuming exogenous prices – it maximises the monetary value of the return from productive activity, this being the sum of the values of leisure, savings, and goods for consumption, all taken in the desired proportions. He makes no argument that the prices chosen by the market are themselves optimal. His paper wasn't translated into English until 1935. It received an approving summary from Samuelson but seems not to have influenced the development of the welfare theorems as they now stand. Abba Lerner (1934) 
In 1934 Lerner restated Edgeworth's condition for exchange that indifference curves should meet as tangents, presenting it as an optimality property. He stated a similar condition for production, namely that the production–possibility frontier (PPF, to which he gave the alternative name of 'productive indifference curve') should be tangential with an indifference curve for the community. He was one of the originators of the PPF, having used it in a paper on international trade in 1932. He shows that the two arguments can be presented in the same terms, since the PPF plays the same role as the mirror-image indifference curve in an Edgeworth box. He also mentions that there's no need for the curves to be differentiable, since the same result obtains if they touch at pointed corners. His definition of optimality was equivalent to Pareto's:
If... it is possible to move one individual into a preferred position without moving another individual into a worse position... we may say that the relative optimum is not reached... The optimality condition for production is equivalent to the pair of requirements that (i) price should equal marginal cost and (ii) output should be maximised subject to (i). Lerner thus reduces optimality to tangency for both production and exchange, but does not say why the implied point on the PPF should be the equilibrium condition for a free market. Perhaps he considered it already sufficiently well established. Lerner ascribes to his LSE colleague Victor Edelberg the credit for suggesting the use of indifference curves. Samuelson surmised that Lerner obtained his results independently of Pareto's work. Harold Hotelling (1938) 
Hotelling put forward a new argument to show that 'sales at marginal costs are a condition of maximum general welfare' (under Pareto's definition). He accepted that this condition was satisfied by perfect competition, but argued in consequence that perfect competition could not be optimal since some beneficial projects would be unable to recoup their fixed costs by charging at this rate (for example, in a natural monopoly). Oscar Lange (1942) 
Lange's paper 'The Foundations of Welfare Economics' is the source of the now-traditional pairing of two theorems, one governing markets, the other distribution. He justified the Pareto definition of optimality for the first theorem by reference to Lionel Robbins's rejection of interpersonal utility comparisons, and suggested various ways to reintroduce interpersonal comparisons for the second theorem such as the adjudications of a democratically elected Congress. Lange believed that such a congress could act in a similar way to a capitalist: through setting price vectors, it could achieve any optimal production plan to have achieve efficiency and social equality. His reasoning is a mathematical translation (into Lagrange multipliers) of Lerner's graphical argument. The second theorem does not take its familiar form in his hands; rather he simply shows that the optimisation conditions for a genuine social utility function are similar to those for Pareto optimality. Abram Bergson and Paul Samuelson (1947) 
Samuelson (crediting Abram Bergson for the substance of his ideas) brought Lange's second welfare theorem to approximately its modern form. He follows Lange in deriving a set of equations which are necessary for Pareto optimality, and then considers what additional constraints arise if the economy is required to satisfy a genuine social welfare function, finding a further set of equations from which it follows 'that all of the action necessary to achieve a given ethical desideratum may take the form of lump sum taxes or bounties'''. Kenneth Arrow and Gérard Debreu (separately, 1951)  

Arrow's and Debreu's two papers (written independently and published almost simultaneously) sought to improve on the rigour of Lange's first theorem. Their accounts refer to (short-run) production as well as exchange, expressing the conditions for both through linear functions. Equilibrium for production is expressed by the constraint that the value of a manufacturer's net output, i.e. the dot product of the production vector with the price vector, should be maximised over the manufacturer's production set. This is interpreted as profit maximisation. Equilibrium for exchange is interpreted as meaning that the individual's utility should be maximised over the positions obtainable from the endowment through exchange, these being the positions whose value is no greater than the value of his or her endowment, where the value of an allocation is its dot product with the price vector. Arrow motivated his paper by reference to the need to extend proofs to cover equilibria at the edge of the space, and Debreu by the possibility of indifference curves being non-differentiable. Modern texts follow their style of proof. Greenwald-Stiglitz theorem

In their 1986 paper, "Externalities in Economies with Imperfect Information and Incomplete Markets", Bruce Greenwald and Joseph Stiglitz showed that the fundamental welfare theorems do not hold if there are incomplete markets or imperfect information. The paper establishes that a competitive equilibrium of an economy with asymmetric information is generically not even constrained Pareto efficient. A government facing the same information constraints as the private individuals in the economy can nevertheless find Pareto-improving policy interventions. Greenwald and Stiglitz noted several relevant situations, including how moral hazard may render a situation inefficient (e.g. an alcohol tax may be pareto improving as it reduces automobile accidents). Assumptions for the fundamental theorems 
In principle, there are two commonly found versions of the fundamental theorems, one relating to an exchange economy in which endowments are exogenously given, and one relating to an economy in which production occurs. The production economy is more general and entails additional assumptions. The assumptions are all based on the standard graduate microeconomics textbook. The fundamental theorems do not generally ensure existence, nor uniqueness of the equilibria. First Fundamental Theorem 

 The preference relation  is locally non-satiated for each consumer i. Agents (consumers and, in a production economy, firms) take prices as given. Markets are complete. Perfect information. Agents behave rationally. Preferences are selfish. No externalities. Second Fundamental Theorem 
The second fundamental theorem has more demanding conditions. All assumptions of the first theorem; in addition:
 The preference relation  is locally non-satiated and convex for each consumer i
 The production set  is convex for each firm j. For the step from price quasi-equilibrium to price equilibrium with transfers: The initial endowment of each agents is strictly positive. Common failures of the assumptions 
The following provides a non-exhaustive list of common failures of the assumptions underlying the fundamental theorems. Price-taking behaviour: In game theoretic interactions, e.g. when firms have monopoly power, the resulting equilibrium is not pareto-efficient
 Externalities: In many instances, prominently pollution & climate action, this assumption is violated. In certain instances, a Pigouvian tax can restore the pareto-efficient allocation. Non-satiation: While non-satiation is a very weak assumption, there exist two primary cases in which it fails to hold. Firstly, if preferences have a satiation point (e.g. Central Banks who target inflation have a satiation point at the inflation rate that they target). Secondly, if goods can only be purchased in discrete chunks, this assumption might be violated. Rationality: The field of Behavioral economics documents many violations of economic rationality. Convexity: In the presence of increasing returns to scale, convexity fails. Note that this assumption is not necessary for the first fundamental theorem. Another instance in which the welfare theorems fail to hold is in the canonical Overlapping generations model (OLG). A further assumption that is implicit in the statement of the theorem is that the value of total endowments in the economy (some of which might be transformed into other goods via production) is finite. In the OLG model, the finiteness of endowments fails, giving rise to similiar problems as described by Hilbert's paradox of the Grand Hotel. Whether the assumptions underlying the fundamental theorems are an adequate description of markets is at least partially an empirical question and may differ case by case. Proof of the first fundamental theorem 

The first fundamental theorem holds under general conditions. A formal statement is as follows: If preferences are locally nonsatiated, and if  is a price equilibrium with transfers, then the allocation is Pareto optimal. An equilibrium in this sense either relates to an exchange economy only or presupposes that firms are allocatively and productively efficient, which can be shown to follow from perfectly competitive factor and production markets. Given a set  of types of goods we work in the real vector space over ,  and use boldface for vector valued variables. For instance, if  then  would be a three dimensional vector space and the vector  would represent the bundle of goods containing 1 unit of butter, 2 units of cookies and 3 units of milk. Suppose that consumer i has wealth  such that  where  is the aggregate endowment of goods (i.e. the sum of all consumer and producer endowments) and  is the production of firm j. Preference maximization (from the definition of price equilibrium with transfers) implies (using  to denote the preference relation for consumer i):

if  then 

In other words, if a bundle of goods is strictly preferred to  it must be unaffordable at price . Local nonsatiation additionally implies:

if  then 

To see why, imagine that  but . Then by local nonsatiation we could find  arbitrarily close to  (and so still affordable) but which is strictly preferred to . But  is the result of preference maximization, so this is a contradiction. An allocation is a pair  where  and , i.e. is the 'matrix' (allowing potentially infinite rows/columns) whose ith column is the bundle of goods allocated to consumer i and  is the 'matrix' whose jth column is the production of firm j. We restrict our attention to feasible allocations which are those allocations in which no consumer sells or producer consumes goods which they lack, i.e.,for every good and every consumer that consumers initial endowment plus their net demand must be positive similarly for producers. Now consider an allocation  that Pareto dominates . This means that  for all i and  for some i. By the above, we know  for all i and  for some i.  Summing, we find:

. Because  is profit maximizing, we know , so . But goods must be conserved so . Hence,  is not feasible. Since all Pareto-dominating allocations are not feasible,  must itself be Pareto optimal. Note that while the fact that  is profit maximizing is simply assumed in the statement of the theorem the result is only useful/interesting to the extent such a profit maximizing allocation of production is possible. Fortunately, for any restriction of the production allocation  and price to a closed subset on which the marginal price is bounded away from 0, e.g., any reasonable choice of continuous functions to parameterize possible productions, such a maximum exists. This follows from the fact that the minimal marginal price and finite wealth limits the maximum feasible production (0 limits the minimum) and Tychonoff's theorem ensures the product of these compacts spaces is compact ensuring us a maximum of whatever continuous function we desire exists. Proof of the second fundamental theorem 

The second theorem formally states that, under the assumptions that every production set  is convex and every preference relation  is convex and locally nonsatiated, any desired Pareto-efficient allocation can be supported as a price quasi-equilibrium with transfers. Further assumptions are needed to prove this statement for price equilibria with transfers. The proof proceeds in two steps: first, we prove that any Pareto-efficient allocation can be supported as a price quasi-equilibrium with transfers; then, we give conditions under which a price quasi-equilibrium is also a price equilibrium. Let us define a price quasi-equilibrium with transfers as an allocation , a price vector p, and a vector of wealth levels w (achieved by lump-sum transfers) with  (where  is the aggregate endowment of goods and  is the production of firm j) such that:

i.  for all  (firms maximize profit by producing )
ii. For all i, if  then  (if  is strictly preferred to  then it cannot cost less than )
iii. (budget constraint satisfied)

The only difference between this definition and the standard definition of a price equilibrium with transfers is in statement (ii). The inequality is weak here () making it a price quasi-equilibrium. Later we will strengthen this to make a price equilibrium. Define  to be the set of all consumption bundles strictly preferred to  by consumer i, and let V be the sum of all . is convex due to the convexity of the preference relation . V is convex because every  is convex. Similarly , the union of all production sets  plus the aggregate endowment, is convex because every  is convex. We also know that the intersection of V and  must be empty, because if it were not it would imply there existed a bundle that is strictly preferred to  by everyone and is also affordable. This is ruled out by the Pareto-optimality of . These two convex, non-intersecting sets allow us to apply the separating hyperplane theorem. This theorem states that there exists a price vector  and a number r such that  for every  and  for every . In other words, there exists a price vector that defines a hyperplane that perfectly separates the two convex sets. Next we argue that if  for all i then . This is due to local nonsatiation: there must be a bundle  arbitrarily close to  that is strictly preferred to  and hence part of , so . Taking the limit as  does not change the weak inequality, so  as well. In other words,  is in the closure of V.

Using this relation we see that for  itself . We also know that , so  as well. Combining these we find that . We can use this equation to show that  fits the definition of a price quasi-equilibrium with transfers. Because  and  we know that for any firm j:

 for 

which implies . Similarly we know:

 for 

which implies . These two statements, along with the feasibility of the allocation at the Pareto optimum, satisfy the three conditions for a price quasi-equilibrium with transfers supported by wealth levels  for all i. We now turn to conditions under which a price quasi-equilibrium is also a price equilibrium, in other words, conditions under which the statement "if  then " imples "if  then ". For this to be true we need now to assume that the consumption set  is convex and the preference relation  is continuous. Then, if there exists a consumption vector  such that  and , a price quasi-equilibrium is a price equilibrium. To see why, assume to the contrary  and , and  exists. Then by the convexity of  we have a bundle  with . By the continuity of  for  close to 1 we have . This is a contradiction, because this bundle is preferred to  and costs less than . Hence, for price quasi-equilibria to be price equilibria it is sufficient that the consumption set be convex, the preference relation to be continuous, and for there always to exist a "cheaper" consumption bundle . One way to ensure the existence of such a bundle is to require wealth levels  to be strictly positive for all consumers i''. See also
Convex preferences
Varian's theorems – a competitive equilibrium is both Pareto-efficient and envy-free. General equilibrium theory

References

Economics theorems
General equilibrium theory
Welfare economics