Each transaction is required to include a gas limit and a fee that the transaction is willing to pay for each unit of gas. Miners have the option to package these transactions and charge these fees.
Today all transactions are ultimately chosen by the miner, but the amount of money the user chooses to pay for a transaction affects the amount of time the transaction has to wait to be packaged.
If the transaction needs to use less than or equal to the set gas limit due to the calculation, including the original message and some other messages triggered, then the transaction will be processed. If the total gas consumption exceeds the gas limit, then all operations are recovered, but the transaction is valid and the transaction fee is charged by the miner. The blockchain will show that the transaction was attempted, but all contract commands were recovered because not enough gas was provided.
So any excess gas in the transaction that is not used is credited back to the transaction initiator in the form of ethereum. Because gas consumption is generally only a rough estimate, many users will overpay for gas to ensure their transactions will be accepted. This is fine, as excess gas will be returned to you.