Basis starts as what you paid plus costs, then changes. A drip adds a new lot at each reinvestment. A return-of-capital distribution reduces basis rather than being taxed immediately. A spin-off splits the original basis between parent and child by relative value. A wash-sale-rule disallowance adds the disallowed loss to the replacement shares' basis.
Brokers report basis to tax authorities for covered securities, but the reporting can be wrong after transfers and corporate actions, which makes your own records worth keeping.
Example: 100 shares at $40 plus $5 commission gives $4,005 basis, $40.05 a share. A return-of-capital of $2 a share reduces it to $3,805. Selling at $52 realises $5,200 minus $3,805 = $1,395 of gain.
Related: tax-lot, long-term-capital-gains, tax-loss-selling, return-of-capital, wash-sale-rule