The ex-dividend date is crucial for investors who want to receive dividends. To be eligible for a dividend, you should have a buy position open before or on the ex-dividend date. If you close it before this date or open it after, you will not receive the dividend.
Even if you close the buy position after the ex-dividend date, you are still entitled to the next dividend payout – but just the upcoming one. If you have a sell position before the ex-dividend date, you will have to pay the dividend.