Question about capital gains taxes

Hi everyone. I am new to investing and have a quick question about taxes.

Let’s say I bought a stock at $50 and sold it in the future at $150. So I gained $100 in profit.

Now I use that $150 to buy a new stock and it goes down to $25 and I sell it. Now overall I lost 25 dollars from the start.

In this scenario do I still need to pay capital gains tax on that initial $100 profit I made? If yes is it possible to pay capital gains tax immediately after selling a stock? Or do I need to wait until I file my yearly taxes?