A payment gateway is the bridge between your website and your merchant account. It is the online equivalent of the machines that retail merchants slide your credit card through.
Your website shopping cart script interacts with your payment gateway in a few different ways. If you are doing real-time transactions, your script will send the credit card information your customers enter to the payment gateway, and the gateway will send back either an authorization or a decline response. Then your shopping cart will tell the customer that their order went through or that their card was declined.
The payment gateway stores those authorizations and once daily it will run a “batch” of all your orders. This turns each “authorization” into a “funds capture” and starts the process of getting that money from your customers credit card or bank account, through your merchant account, and deposited into your bank account.
If you catch a transaction before it has been batched, you can void it, otherwise you will need to refund it. Note that when you refund a transaction, you get charged the discount rate again–you don’t get it refunded. This is not as bad as chargebacks though, where you get the money taken out of your account, and you get charged around $25 for the chargeback happening at all. So, good communication with your customers and fraud detection software is important.
The newest generation of merchants like Stripe are making the merchant account / payment gateway combo obsolete by providing both in one and saving you money in the process.