I put a limit buy order for 1000 shares at $1.26, and another investor has also put a limit buy order in for the same amount and volume. If someone sells 1000 shares at $1.26, which one of us gets the shares?
The one whose order gets to the exchange first. The exchange receives the orders and arranges them in First-In-First-Out order, by which they're then executed. At some point it is synchronized and put into a list. Whoever gets to that point first - gets the deal.
While littleadv's answer is true for many exchanges (in particular the stock market, it's called FIFO matching) you should also know that some markets trade pro rata. That is, for a match at some price level everyone at that level gets a chunk of the deal proportional to their input (i.e. order size).
E.g. match for quantity
X at a price level and passive side orders
y2; the order
y1 would get
y1 / (y1 + y2) of
y2 would get
y2 / (y1 + y2) (for
X = min(X, y1 + y2)).