Your answer is actually in your post. Here's the simplest way to understand.
If you invest and make $20,000 profit and it's just sitting in your ETRADE account (or which ever you have) on Dec 31st, than you pay taxes on that $20,000 profit. For me in Pennsylvania, it' would be 25% Federal and 7% state (assuming I sold the stock in less than a year).
However, if you sell a stock and make $20,000 profit but than buy a different stock (or the same one for that matter) and you lose $20,000, putting you right back where you started from, than you have no Capital Gains to pay. It's a push...or break even.
If you hold s stock for over a year in Pa and most states (some states have different rates), than you only pay 21% on your Federal tax but still 7% state tax (or whatever your state tax rate is).
If you live in Florida or Texas, that don't have state taxes (I believe there is 1 other state that doesn't have state taxes).