An externality is defined as an uncompensated impact of the actions of one person on the wellbeing of a third party. A positive externality is an externality that makes the third party better off. This creates a problem because the person creating the positive externality has no incentive to take into account the fact that they also creates benefits for other people. This leads to a situation where the market produces too little of a good, relative to the quantity that would maximise total wellbeing (or total welfare).
This is illustrated in the diagram below, which shows a positive consumption externality. The marginal social benefits (MSB - the benefits that society receives when people consume the good) are greater than the marginal private benefits (MPB - the benefits that the individual receives themselves by consuming the good). The difference is the marginal external benefits (MEB - the benefits that others receive when a person consumes the good). The market will operate at the quantity where supply meets demand, which is QM on the diagram. However, total welfare is maximised at the quantity where marginal social benefit is equal to marginal social cost, which is QS on the diagram. The market produces too little of this good, because every unit beyond QM (and up to QS) would provide more additional benefit for society (MSB) than what it costs society to produce (MSC). However, the buyers have no incentive to take into account those external benefits, so they don't consume enough.
What does that have to do with trade unions (as in the title of this post)? When a person belongs to a trade union, that provides them with some private benefit (MPB) - they can call on the union if they have a problem with their employer, they can use the union to negotiate for better pay and conditions on their behalf, and so on. However, a person's union membership also creates benefits for others (MEB), because the more people who are union members, the more negotiating power the union will have. So, it seems clear that in the case of unions, the marginal social benefits exceed the marginal private benefits, and the market for union membership will lead to too few people being members of trade unions (just as in the diagram above).
When a positive externality leads a market to produce too little of a good, we could rely on the Coase Theorem, which suggests that when private parties can bargain without cost over the allocation of resources, they can solve the problem of externalities on their own. In the case of unions, the Coase Theorem suggests that employees should be able to develop a solution to the externality that leads to the 'right' number of people becoming union members. However, the Coase Theorem relies on transaction costs being small, which is not the case when there are a large number of parties involved (which is the case when there are many employees). If the Coase Theorem fails, then that leaves a role for government.
Public solutions to a positive externality problem could be based on a command-and-control policy. That is, a policy that regulates the quantity. Compulsory union membership would be a potential command-and-control solution to the positive externality problem, but it seems unlikely that the quantity QS in the diagram above is equal to (or more than) every person belonging to a union.
In most cases of positive externalities, the government relies on a market-based solution to positive externality problems, such as providing a subsidy. The effect of a subsidy on the market is illustrated in the diagram below. The curve S-subsidy illustrates the effect of paying a subsidy to the trade union for every union member (you could achieve the same effect by partially reimbursing every union member - a subsidy on the demand side of the market). This lowers the price of union membership for members to PC (which incentivises more people to join the union), and raises the effective price for the unions to PP. The quantity of union membership increases to QS, which is the optimal quantity of union membership.
Many governments like to subsidise their favoured sectors of the economy, even though that lowers total welfare. Perhaps they should be looking to subsidise trade unions instead?