It does not seem practical to scan every transaction on a block chain to determine the balance for an account. As the block chain grows this seems even more difficult and resource consuming. Perhaps all the transactions are grouped into what we used to call a record, specifically a variable length record. address..trans1, trans2, etc...transN for the last transaction.
Bitcoin uses the so called UTXO (Unspent Transaction Output) model.
The first time it is not very intuitive because it is different from the traditional accounting model where you just spend from and send to an account.
Think about full piggy banks that can't be filled more than they are and that you have to break open before being able to spend the content:
1) Bob has 1 piggy bank, he wants to pay Alice with half of the piggy bank's content
2) he breaks his piggy bank
3) he takes half of it and gives it to Alice, who will put it all in a new piggy bank belonging to her 4) you put the remaining half in a new smaller piggy bank belonging to you and which is waiting to be broken open.
Each piggy bank/UTXO is linked to an address.
A Bitcoin user can of course have several UTXOs related to his address. The sum of the UTXOs gives the amount of bitcoins owned by the address (which is one of the function of wallets).
Each transaction input is the output of a previous transaction. Although wallet software gives the feeling that you "transfer" bitcoins or satoshis, you don't spend from an account you spend UTXO.
You spend from transactions previously made to you.
A somewhat more intuitive way to think about balance calculations is to think of you you use an actual fiat wallet.
If you want to look at how much money you physically have right now, you would open up your wallet, take out all the notes of varying denominations, and sum them up. This gives you your balance.
Bitcoin does something similar, where an address is similar to your physical wallet. A single address may have multiple utxos linked to it. The balance for that address is the sum of all the utxos.
The key thing here is that bitcoin only tracks unspent transaction outputs. Thus, to arrive at the balance for the address, it just needs to iterate through the blockchain, add new outputs when they are created, and remove old ones when they are spent. Moreover, this is a extendable process. If you know the utxo set as of block 100, you do not need to recalculate from block 0 to update with data from block 101. You can simply update your set from block 100.
This again is similar to how we use physical wallets. No one keeps track of all the notes and coins they have had in the past (spent transaction outputs). They only keep track of what they have currently (utxo). When a new transaction occurs (say you buy milk), you only update the current state of your wallet by removing a note, and putting the change back. You don't need to calculate it from the first time you used money.
Bitcoin takes this a step further and has a concept of accounts, which are multiple addresses that act as one wallet. A similar analogy would be if everyone in your house pooled the money from all of their wallets, so that you can spend it as one. Again, the balance of an account is simply the sum of the balance of the addresses in it.
Address Balance Calculation
An address balance is calculated by adding up all of the values of the unspent transaction outputs (UTXOs) that are spendable by the key pair corresponding to that address.
To answer the broader question: "why do you scan every transaction on the entire blockchain to determine your balance?"
Bitcoin core does not need to know the balance of an address in order to do validation. It does, however need to be able to scan through all previous transaction outputs in order to validate new ones. To do this, it scans a generated indexed database (by txid) of all transactions for the previous outputs referenced by a new transaction, and verifies that the signature satisfies the previous output (UTXO). Note this only requires scanning unspent outputs not all transactions.
Optimizing this data storage for balance calculation by indexing by address and txid might make balance calculation easier, but it isn't necessary for validation and would be at the expense of transaction verification efficiency. It is better to leave this to a wallet application for the sake of the network.
The previous answer explains how to index transactions in bitcoin core which can help a wallet or block explorer determine an address balance more efficiently.
You can index the transactions on the blockchain to make it more easily searchable. Bitcoin-core allows this using the
From the Bitcoin Wiki:
txindex | Maintain a full transaction index, used by the getrawtransaction rpc call (default: 0)