When you arrive in country from overseas the immigration officer checks your passport.
In some countries you must have visa before you can enter.
Then you proceed to the baggage area to pick-up your bags and then to the customs officer who may,(but not always)check your baggage.
When you leave a country you must first check in with the airline to get your boarding pass.
If there are a lot of people you must queue Then you go through immigration,where they check your travel documents and then to the departure lounge to wait for your flight.