When you make a bitcoin transaction, it goes into a pool of unconfirmed transactions. Then, bitcoin miners select your transaction and place it into a block of transactions. The miner solves a special mathematical puzzle called a proof of work. After that, the Bitcoin Network confirms your block and adds it to the blockchain. Each new block added to the blockchain is another confirmation for your transaction.
Because you need an enormous amount of computing power to solve these puzzles, they are difficult to undo. This is because each puzzle builds upon the previous blocks so to get to block #2, you would need to also undo blocks #7, #6, #5, #4, and #3. This would take a nearly impossible amount of computing power, so your bitcoin is as secure as possible after six confirmations.
Sometimes, your transaction will be unconfirmed for a long period of time. This is likely because you did not include a large enough bitcoin miner fee or were using a bitcoin exchange to pay (exchanges do not attach bitcoin miner fees). Miner fees are like the shipping you pay when ordering something online. They encourage the miner to select your transaction. Bitcoin exchanges often try to send their transactions as efficiently as possible. This can mean that the fee is not large enough or doesn’t arrive quickly enough for a miner to include your transaction in the next block.