In more detail
The gain is the sale proceeds minus the cost basis, which is the purchase price plus buying costs such as commissions; selling costs reduce the proceeds. A gain is unrealised while you still hold the asset and realised when you sell. Many countries tax only realised gains, and some tax gains on assets held a long time at lower rates; in the US the line is holding for more than one year. Realised losses can usually offset realised gains.
Why it matters
Forgetting fees or reinvested dividends in the basis overstates the gain and the tax. Selling a few days before a holding-period threshold can move a gain into a higher-taxed category, and the timing of sales across tax years changes what you owe.
Example
You buy 100 shares at $40 and pay a $10 fee: basis $4,010. You sell them at $55 and pay another $10: proceeds $5,490. The capital gain is $5,490 − $4,010 = $1,480. If you also realised a $600 loss on another holding in the same year, the net gain is $880.
Common confusion
A dividend is income paid while you hold a share; a capital gain arises only from the change in price when you sell.