You cannot trust the balance that the ATM gives you. There banking system operates on a system of holds, while money is transferred from your account to other places. Even your debits are not automatically deducted from your checking account. For this reason it is important to keep a running balance of your account at all times.
When you make a deposit the bank gives you credit for the deposit, but the check is set to a clearinghouse. The clearinghouse is regional, and if the check is from out of state it may need to be sent to another clearinghouse, before the bank actually receives the money for your check. When you deposit a large check from out of state the bank may put a hold on it until funds are collected. This protects you from spending the money before you actually get it. If you do this you will have to pay the bank back.
Similarly checks do not clear your account the moment that you write them and send them off in the mail. Once the person or company receives your check, they must deposit in their bank and then wait for the money to be transferred from your account. Checks usually take the longest to clear, because it depends on how quickly they company takes the checks to your bank.
Debits are not automatically deducted from your account. When you first use your debit card a hold is placed on your account. This hold lasts a few days, and then will drop off. The merchant that accepted your debit card has to send in its transactions in order for the debits to be transferred to them. Although most merchants do this on a daily basis, some small businesses may take longer. When this happens the hold may drop off before the amount is deducted from your account, and your balance would say that you have more than you do.
Since you have all of these factors working against you, you should keep a running balance of your account. You cannot simply trust the ATM balance to be the correct one. Similarly just because a debit transaction goes through, it does not mean that you currently have the money to cover the transaction. To keep a running balance you simply record your transactions as you go, then you add and subtract them from your balance in order to get the amount that you really have.

