Contracts do not transact. Externally owned accounts (users) make transactions.
A user can make a transaction as a sender, to a contract (recipient). In this case, a user wants a contract's specific function to be run and change the world state. The transaction is signed by the user's private key. So the miner hears about the transaction and facilitates the user's request. The miner runs the code to get the next state of the Ethereum blockchain.
After the miner publishes a block that includes the above transaction, every full node in the ecosystem hears about the new block and runs the code for themselves (for every transaction in the new block) to get to the new state and reach consensus (agree with the rest of the world on the state)
The code that the user asks to be run could send funds to the contract or withdraw funds from the contract or whatever logic can be implemented with Solidity.
The implementation of the function should check whether the user is allowed to run the code. In any case of error, e.g. out-of-gas error, data type overflow error, divide by zero error, array-out-of-index error, etc. the miner will include the transaction in the block and the rest of the nodes will verify that the transaction happened and failed, so no change of world state has occurred.