In most cases, you’ll go through customs and immigration after your flight first arrives in a new country, but there are exceptions.
For example, some countries have mutual agreements intended to help speed travelers through the process, so you may go through customs and immigration before you board. When flying from Ireland to the United States, for instance, you’ll go through US customs in Ireland. But when arriving to the US from most other countries, you’ll go through customs upon your first arrival in the US (even if you have a connecting flight onward).
And sometimes you don’t go through customs until you reach your final destination. For example, if you’re flying from the US to Europe and have a layover in one Schengen Area country before you arrive in a second Schengen Area country, you’ll typically go through immigration when you first land, but you won’t go through customs until you reach your final destination.