The following is a simplified explanation of what geth does.
There are two main places where transactions can be: in a block, or in the transaction pool (txpool).
When a node first hears about a transaction, it puts it in the txpool. These are essentially zero confirmation transactions. There's a number of ways to access this, most particularly the "pending" psuedoblock in web3.
When a node starts to mine, it will take the highest-paying transactions in the txpool, one-by-one, and execute them. When it runs out of the gasLimit or transactions in the pool, it commits to mining that specific block. (This appears in the console as something like "commit new work on block X with Y txs...") It then continues hashing that block until it finds a hash that is sufficiently rare enough for the block to be accepted by the network.
Every other transaction, whether it was received before it started work on the block but could not use it for some reason, or whether it received it afterwards, remains in the txpool until it is mined, or becomes permanently invalid for some other reason.