What will happen if I short a stock the day before the ex-dividend date and then buy it back on the ex-dividend date?

Because you are technically borrowing from another shareholder when you short a stock you will be required to pay the dividend out. If you have an online trading account this will happen automatically. The dividend amount will be withdrawn from your account.