How Does a Double-Spend Transaction Work in the Context of a 51% Attack?

In a double-spend attack, the attacker first sends coins to an exchange and waits for a few confirmations, then sells or withdraws them. Simultaneously, the attacker secretly mines an alternative chain where the transaction to the exchange is replaced with a transaction sending the coins back to their own wallet.

Once the secret chain is longer than the public one, the attacker broadcasts it. The network adopts the longer chain, nullifying the exchange transaction and effectively "double-spending" the coins.

How Do ‘Reorgs’ (Reorganizations) in a Blockchain Relate to the Success of a 51% Attack?
How Does a Successful 51% Attack Lead to ‘Double-Spending’?
What Is the Significance of the “Longest Chain Rule” in Executing a Double-Spend Attack?
Can a Finney Attack Be Used to Double-Spend in a Proof of Work System?
What Is the Difference between a Soft Fork and a Hard Fork in the Context of Changing a Mining Algorithm?
What Is a ‘Reorg’ (Reorganization) and How Is It Used in a 51% Attack?
What Is a Soft Fork, and How Does It Differ from a Hard Fork in Blockchain Upgrades?
What Is a “Time-Bandit” Attack in Relation to Double-Spending?