A transaction first has to be verified by the network. It is gradually passed round the network and each full node checks to see whether, subject to certain criteria, it is a valid transaction.
When the transaction is verified it is then placed in a transaction pool. At this point the miner then selects – up to the maximum block size – a number of transactions from the pool.
Transactions are chosen by priority which is defined as larger trades and oldest trades – so the older the transaction and the larger the transaction the more likely it is to be picked out of the pool.
The first 50 kb of any block is set aside for high priority transactions, otherwise the miner can just choose the largest number of transactions with fees – and low value old transactions would never get processed.
Any transactions left over would sit in the pool ready to be picked up and hashed into the next block.
The transactions chosen would then be fed into a Merkle tree and used with all the other required data by the miner to find a solution.
If a solution is found the miner then sends the solution and all its required data to its neighbouring node who then checks the calculations, and if they are correct, forwards it on to its neighbour. However, if two solutions are found at the same time a fork can occur in the Bitcoin blockchain – or basically a temporary variation across the network.