Every now and then, blocks become orphaned during what is called blockchain reorganization. This happens when two valid blocks are found at about the same time, and the network is split on which one is considered the top of the blockchain. These two blocks can contain different sets of transactions, so one block may contain your transaction while the other one doesn't.
This situation will be automatically resolved. Let's call the two blocks A and B. Eventually, a new block will be found on top of one of the two competing blocks. Let's say this new block, C, is found on top of A. When C is broadcast, any nodes recognizing the B as the tip of the chain will see that more work as been done on the other fork, and will orphan B in favor of A and C. It will be like B never happened. If your transaction was confirmed as part of B, it will be like your transaction never happened.
The good news is that it is likely that A and B share a good deal of the same transactions. Even though you may have been recognizing B instead of A, there's a good chance the transaction will be already confirmed in A when you come around to recognizing it. If not, it means the transaction will still be in the mempool, and is likely included in C or will be included in a later block.
As you can see, the odds of this happening go down as each new block piles on top of the block containing your transaction. By the time 5 blocks have been mined on top of the block containing your transaction (thus 6 confirmations) there is no reasonable chance of that block being orphaned. The transaction is now permanent.