This answer is about the USA.
Each time you sell a security (a stock or a bond) or some other asset, you are expected to pay tax on the net gain. It doesn't matter whether you use a broker or mutual fund to make the sale. You still owe the tax.
Net capital gain is defined this way:
Gross sale prices less (broker fees for selling + cost of buying the asset)
The cost of buying the asset is called the "basis price." You, or your broker, needs to keep track of the basis price for each share. This is easy when you're just getting started investing. It stays easy if you're careful about your record keeping.
You owe the capital gains tax whenever you sell an asset, whether or not you reinvest the proceeds in something else. If your capital gains are modest, you can pay all the taxes at the end of the year. If they are larger -- for example if they exceed your wage earnings -- you should pay quarterly estimated tax. The tax authorities ding you for a penalty if you wait to pay five- or six-figure tax bills without paying quarterly estimates.
You pay NET capital gains tax. If one asset loses money and another makes money, you pay on your gains minus your losses. If you have more losses than gains in a particular year, you can carry forward up to $3,000 (I think). You can't carry forward tens of thousands in capital losses.
Long term and short term gains are treated separately. IRS Schedule B has places to plug in all those numbers, and the tax programs (Turbo etc) do too.
Dividend payments are also taxable when they are paid. Those aren't capital gains. They go on Schedule D along with interest payments.
The same is true for a mutual fund. If the fund has Ford shares in it, and Ford pays $0.70 per share in March, that's a dividend payment. If the fund managers decide to sell Ford and buy Tesla in June, the selling of Ford shares will be a cap-gains taxable event for you.
The good news: the mutual fund managers send you a statement sometime in February or March of each year telling what you should put on your tax forms. This is great. They add it all up for you. They give you a nice consolidated tax statement covering everything: dividends, their buying and selling activity on your behalf, and any selling they did when you withdrew money from the fund for any purpose.
Some investment accounts like 401(k) accounts are tax free. You don't pay any tax on those accounts -- capital gains, dividends, interest -- until you withdraw the money to live on after you retire. Then that money is taxed as if it were wage income.
If you want an easy and fairly reliable way to invest, and don't want to do a lot of tax-form scrambling, choose a couple of different mutual funds, put money into them, and leave it there. They'll send you consolidated tax statements once a year. Download them into your tax program and you're done.
You mentioned "riding out bad times in cash." No, no, NOT a good idea. That investment strategy almost guarantees you will sell when the market is going down and buy when it's going up. That's "sell low, buy high." It's a loser. Not even Warren Buffett can call the top of the market and the bottom. Ned Johnson (Fidelity's founder) DEFINITELY can't.