Coinbase has several wallets and private keys. The private keys are required to send the coins between their wallets and to wallets they don't control.
When you buy bitcoins, Coinbase credits your account when the money is received from your bank. No bitcoins are moved.
When you deposit bitcoins in your Coinbase account, you are sending to a deposit address in Coinbase's wallet. When Coinbase receives the bitcoins, they credit your account. They know which account to credit because only your account uses that deposit address.
When you send bitcoins from your Coinbase account to a friend that has their own wallet, Coinbase debits your account and then uses their private keys to send the bitcoins from their wallet to your friend's wallet.
When you send bitcoins from your Coinbase account to your friend's Coinbase account, Coinbase simply debits your account and credits your friends account. No bitcoins are moved.
When you sell bitcoins in your Coinbase account, Coinbase simply debits your account and transfers the money to your bank account. No bitcoins are moved.
I hope that makes everything clear.