The ex-dividend date is the stated date that you must own a stock in order to collect on the very next dividend that company is offering. If you purchase the stock after that stated date you will not receive the next dividend. Instead, you will be eligible for the dividend following that dividend. This will often happen around 3 months later as most dividends are paid quarterly
One of the more important facts to understand about an ex-dividend date is that you are always eligible for the dividend if you own the stock when that day is declared. This means that even if you sell all of your shares the day after the ex-dividend date is declared, you will still receive the dividends. If this happens and you are reinvesting your dividends it will automatically purchase the amount of shares it would have if you had still owned the stock.
Shareholders need to be very careful of this as if it happens you may end up with stocks of the company that you just sold and will encounter more commissions to get rid of that stock. To prepare for this situation simply turn off your dividend reinvestment before you sell a stock that pays dividends. This will instead send you the cash dividend which you can then withdrawal or use to purchase other stocks.