The ex-dividend date is the day that a stock trades without its dividend. In order to qualify for the dividend you must own the stock at the end of trading the day before the ex-dividend date. If you buy a stock on any day before the ex-dividend date and sell on the ex-dividend date (or any day thereafter) you will get the dividend.
On the ex-dividend date the stock price will adjust lower to balance out the dividend that was paid per share. Let’s say, for example, that a particular stock closed at $10 on the day prior to the ex-dividend date. If that stock was to pay a $.50 dividend the stock would then open at the adjusted price on the ex-dividend date. The adjusted price would be $9.50.
To better understand how ex-dividend dates work – See the SEC’s explanation of ex-dividends.