The key insight from Adam Smith was that each person pursuing their own self-interest could promote the general welfare of society. To see that this is a true statement consider the supply-and-demand framework below.
Demand (willingness to pay) slopes downward because at lower prices there are more people "willing to pay". Supply (willingness to accept) slopes upward because at higher prices there will be more people "willing to accept" the higher price. The intersection of the two lines represents the market-price.
Willingness to Pay - Price = Consumer Profit,
If we add up all the consumer profits ==> Consumer Surplus
Price - Willingness to Accept = Producer Profit,
If we add up all the producer profits ==> Producer Surplus
"Surplus" is just a fancy way of saying "the value created". We know that trades only occur when both parties receive a positive profit. Therefore, each person, not necessarily looking out for the interest of others will create value when transactions happen. But, this is all based upon price.
In a market economy priority (who buys and sells the goods and services) is based on willingness to pay being greater than price and price being greater than willingness to accept.
Without getting into too much more depth at present a price tag is difficult to place on some goods that people find very meaningful. Therefore, priority cannot be established through the price mechanism. Instead, we must determine how goods and services are allocated through non-price rationing. Because price is not establishing priority for the allocation of goods and services we must be guided by some other normative principles of justice. This is why I talk about justice in class even though I am no expert in philosophy.