Other than the above situation, an Agent is paid commission by the seller according to what stated on the listing agreement (which has to be submitted along with the purchase agreement). However, the reality is that the lenders, while negotiating the short sale, can potentially consider the commission paid by the seller and negotiate on that.
For REO (Bank owned), the seller is the bank, so the seller/bank will pay the commission as agreed.
In general, neither case the buyer pays, unless there is special agreement in place - it's all in the details of the contract. which you should read very carefully - the banks have their own contracts which can be quite different from conventional contracts.
The answers given are good, however, a recent Caifornia Supreme Court decision to not hear an appeal of Schweitzer vs. Westminster, should allow agent/brokers to get a commission on a foreclosure property (when listed or possibly with a written offer if not listed). It also should be noted that I have seen commissions of more than 3% for selling agents on REO's and also some agents get less than 3% on Short Sales.
All the best,