Let me walk you through the lifecycle of a transaction, and I think it'll become clearer.
A sender submits their transaction to the network. Every node checks whether the transaction is spending existing funds and whether it is otherwise valid. If so, they relay the transaction to their peers. This way, transactions disseminate to all participants in the network.
Some of the network participants, miners, continuously attempt to author new blocks. They draftassemble a block from the valid unconfirmed transactions they receive from their peers. They can choose any subset of the transactions they know about, but generally choose the transactions with the highest feerates to maximize their block reward. They can choosecould for example also prefer to include their own transactions, not include any subset of the transactions they know about, or instead pick transaction by any other pattern (e.g. oldest first?)—this happens in traces, but generally choose the transactions withusually reduces the highest feerates to maximizerevenue of miners, so works against their block rewardinterests.
Using their draft for the next block, their block template, they repeatedly slightly change a field that takes a random input, the nonce, to see whether they can find a valid block using their template. When a miner exhausts the nonce space, they mix up the template in a different manner.
Once a miner discovers a block template for which the hash is below the difficulty target, they broadcast the new block to the network, extending the blockchain. All the transactions in the block are considered to have gotten their first confirmation and are now part of the blockchain. The successful author of the block pays the block reward out to themselves in the so-called coinbase transaction, the first transaction in the block. The reward can be spent when it has 100 confirmations.