For example, if the ex-dividend date is next Monday, and I buy the stock on next Monday, would I get the dividend? Or would the person who sold the stock to me get the dividend?
Here is the definition of Ex-dividend date from the SEC:
Once the company sets the record date, the stock exchanges or the National Association of Securities Dealers, Inc. fix the ex-dividend date. The ex-dividend date is normally set for stocks two business days before the record date. If you purchase a stock on its ex-dividend date or after, you will not receive the next dividend payment. Instead, the seller gets the dividend. If you purchase before the ex-dividend date, you get the dividend.
The linked document discusses weekend, and holidays involved in the calculation.
The difference between the record date and the ex-dividend is to allow for the three days of settlement.