What exactly decides the execution order of transactions within a block? I have read here that baking fees matter, but I am not sure how in particular.
Transactions within past blocks don't seem to be ordered by baker fees. As I understand it, fees matter because a baker wants to maximize the fees it can get for the block it bakes. Does this mean that fees only matter if there are too many pending transactions to fit in a block? When this is the case, bakers would try to fit transactions with a high (baker fee) / (gas used) ratio first, to maximize fees per unit of gas, since there is a gas limit per block.
Currently, blocks only use about 5% of the maximum amount of gas available per block according to tzstats.com. Does this mean that all pending transactions can be included in a block, and therefore fees don't affect ordering?
Does the timing of publishing a transaction within a block matter? In other words, as long as a transaction is executed in the window of a block being built, does the exact timings within that window affect the order of transactions in the block?
I am also aware that bakers are incentivized to order their own transactions favorably.