At immigration (if they have it) they need to check if you overstayed, record your departure, and stamp you out (potentially). At the gate, they're double checking that you're you, matching it to your ticket, and again, ensuring that your passport is valid etc.