What happens if a passenger passes through the boarding gate, but doesn't get on the aircraft? At one time, I was bored because of a delayed flight and that question arose in my mind just for curiosity. I haven't observed any mechanism that checks or counts the passengers who get on the aircraft, so there is a possibility of abuse.

I guess airport security is responsible for that. The passenger may try to get on another aircraft or may get in the airport through a different gate. It must be a bigger problem at international airports and terminals.

If the passenger is caught, what does airport security do, just arrest? What does the airline do if they realise the incident before or after the take-off?
Passengers are counted before the plane leaves the gate. Since everyone's boarding pass is scanned at the gate, they know how many passengers should be on board. If that number does not match, it may be a red flag that someone has sneaked out.

The missing passenger can usually be located as using the lavatory, changed seat etc. As a last resort, the passenger's luggage is removed from the cargo hold for security. If things get to that stage, the flight is inevitably delayed for sure.