The Paypal IPN page is actually provided by paypal, and its main purpose is to validate the transaction by passing a transaction id key from paypal to the page.
if this is a valid key, it then checks if the key already exists, (payment already made)
if not, then the transaction is deemed valid, and paypal code can insert rows into the order and order details tables.
All I did was piggy back some code onto this page by running a couple of update queries within the 'valid transaction' loop.
One query updated the stock level by subtracting the quantity of each item in the cart when the order was placed
The second query updated the order status from 'pending' to 'paid'
I'm afraid I haven't any experience of other payment gateways, but I would imagine the process should be similar.
Once the payment has been successfuly taken, this is the point where I would recommend the stock and order status are updated.