I think this is how the GE functions
Scenario 1: Limited Stock on GE
-Buyer A buys for 30 gp
-Buyer B buys for 50 gp
-Seller sells for 40 gp.
Buyer B would get the item
Seller would get an addition 10 gp
From RS Manual
http://www.runescape.com/kbase/guid/controls_the_grand_exchange#buying_items
Scenario 2: Overstock of GE
-Seller A sells for 30 gp.
-Seller B sells for 50 gp.
-Buyer buys for 30, saving 20 gp
Seller A would sell the item
Note: If two people place the same offer; GE creates a priority list. The first buyer would get the items while the second buyer has to wait until the first buyer's limit is reached or his order is done