Relationship to Costs
Externalities are in essence, completely based around cost. Whether the avoidance of a negative externality causes the cost of a good to increase, or a positive externality causes the society to avoid the potentially high cost of a product. The best way to further explain this relationship would be through the following example:
Suppose because of the nuclear meltdown that occurred in Japan, the United States government issues more strict laws that ensure protection of the people living in a radius 'x' amount of miles from a nuclear power plant. For the case of this example, lets say the additional protection forces the power plant to add thicker lead walls to the inside of the units, and this costs them $10,000 (Not exact). To reflect the additional cost the nuclear plant has to pay to avoid the potential negative externality of nuclear pollution, they must raise the price they charge for the power generated at that plant. This drives the cost up for the consumers directly.
Suppose because of the nuclear meltdown that occurred in Japan, the United States government issues more strict laws that ensure protection of the people living in a radius 'x' amount of miles from a nuclear power plant. For the case of this example, lets say the additional protection forces the power plant to add thicker lead walls to the inside of the units, and this costs them $10,000 (Not exact). To reflect the additional cost the nuclear plant has to pay to avoid the potential negative externality of nuclear pollution, they must raise the price they charge for the power generated at that plant. This drives the cost up for the consumers directly.
Relationship to Deadweight Loss
The relationship externalities has with deadweight loss is very similar to that of costs, because they all are a part of the same system. Deadweight loss is defined by the loss of efficiency that occurs when equilibrium isn't optimal by economic standards. Taking the above example into play, it is obvious where deadweight loss could occur. Upon shifting the supply curve to the left to compensate for the new cost, the optimal level of output is displaced. This will cause deadweight loss to form a wedge between the new level of output and the previous one, showing the amount of money they're losing. The loss comes from customers who chose not to use their service any more because of the increase in price.