Bitcoin is not stored on a machine, but it is kept in a distributed public book known as blockchain. The blockchain is a database that is replicated on the machines of all who participate by running the bitcoin software. This means that the database you have locally on your machine is only a copy of which the network as a whole agrees that it is the correct and indisputable accounting book.
The process by which all participants agree which chain is the true valid chain is called mining, which uses a Proof of Work mechanism to allow a participant to confirm a new block in the chain approximately every 10 minutes. The process by which a participant is selected is pseudorandom and probabilistic, depending on the amount of job they do, which generally corresponds to the amount of electricity that they irreversibly consume to perform the calculation at the current tip of the chain.
The cost of electricity to get the job done is one of the theoretical elements of the Bitcoin game that keeps it safe, because it means that any participant who tries to adjust the ledger to favor will have to spend money and consume electricity, but their chances of success It is extremely small, since they would need to perform more than 50% of the total global work done at the current end of the blockchain to stay ahead honest majority. Most participants are honest because it can be profitable to be honest, but being dishonest generates losses unless you are sure you have more than 50% of the global work done in Bitcoin.
The chain is secured by cryptographic hashes, so any attempt to change anywhere in the ledger produces different hashes. Each block in the chain explicitly refers to the hash of the header of the previous block, and this value is also included in the header of the current block. This means that each block is committed to all blockchain history, and any attempt to rewrite part of the ledger would require rewriting the entire ledger history from the point of modification. Therefore, rewriting the history would require a greater amount of work than the total amount of work that was done between the block that was attempted to modify and the current point of the chain.
The ledger is a linear sequence of blocks that contain a set of transactions, where one of the transactions in the set is a coin base, which charges the fees of other transactions in the block and releases some previously unused bitcoins as part of a default launch program. Currently about 35/40 of all bitcoins have been launched and the remaining 5/40 will be released gradually over the next 120 years.
The other transactions in a block are regular transactions that have inputs and outputs. The entries of a transaction refer to the output of a previous transaction. The outputs contain a lock script, which is a set of prerequisites that must be met for that exit to be spent. Once the exit of a transaction has been spent, it cannot be spent again. This means that there is at any time, a set of exits of transactions not spent (UTXO) in the ledger, which is where all existing bitcoins are stored.
The relationship with a wallet is where this lock script comes. For all regular transactions, one of the prerequisites for unlocking the output and spending it is to provide a digital signature that matches a public key that is stored in the exit lock script. Only the person who has the private key corresponding to the public key is capable of producing said signature, and the message they sign is a hash of the transaction they are trying to spend, which guarantees that no one else can manipulate this transaction because they would invalidate the signature.
Therefore, a wallet is essentially a set of private keys that are used to unlock some bitcoins stored in some UTXO in the distributed public book. A wallet will compare the corresponding public key set with the entire UTXO set of the public ledger to determine which bitcoins are essentially "property" of this wallet. The amount of bitcoin in the UTXO set that the user can unlock corresponds to his balance.
A normal bitcoin software will get a copy of the entire ledger and listen if other nodes add new blocks, which transmit them through a point-to-point network. Several different connections are used to ensure that peers do not present false or outdated information. Each time a new block is created, approximately one every 10 minutes, the wallet will download the block and look through its set of transactions in search of which the wallet has the relevant private keys to unlock. Everything you can unlock is added to the balance, and this becomes again received bitcoin
To spend bitcoins, a user must select one or more UTXO for which he has the private key to unlock. They create a new transaction that refers to these UTXOs as inputs, and create a set of outputs that contain new blocking scripts, usually by including the public key hash of the person they wish to pay, which comes from bitcoin speak to They use for that payment. The amounts of bitcoin at the outflows of a transaction must be less than or equal to the amounts of the incoming UTXOs referred to in the transaction, and any difference is paid as the fee for the transaction. Then, the user will sign this transaction and transmit it through the point-to-point network.
A transaction must be valid for any other node in the p2p network to accept it. This means that the participant who receives a transaction will compare the accuracy of the UTXO set with their own copy of the ledger. If you modify something in your local copy of the ledger, it will not produce changes in the copy that no one else has made. If you tried to spend an amount that other participants do not agree with your property, then your transaction will be invalid and rejected by the network. It will never be included in a block, and the recipient will not receive anything.
In other words, any attempt to modify the data locally can only attempt to deceive itself, but it cannot deceive others. No one accepts anything you say as true: they verify it for themselves by verifying the historical accuracy of the ledger.