How is the iPhone 7 waterproof if its charging port is exposed?
The iPhone 7 series is not waterproof — no smartphone is. According to Apple, the iPhone 7 and iPhone 7 Plus have an IP67 rating for dust and water-resistance. You can read more about how IP ratings work here.

In a nutshell, as far as water is concerned, an IP rating of 67 means the iPhone 7 and iPhone 7 Plus can be fully submerged in up to 1 meter of water for up to 30 minutes without being damaged.

So, will an iPhone 7 die if it takes a brief dip in water that’s deeper than 1 metre? No. 

Will it survive for over a half hour or so in about 5cm (2") of water? Yes. 

What this all means is that an iPhone 7 will survive a water accident.  

As for protecting the Lightning port from water, while I haven't personally seen inside an iPhone 7 yet, what I can tell you is how smartphone manufacturers make their USB and headphone ports water resistant.

Typically, they seal all parts that could let water in. This means the SIM card tray has a built-in rubber seal to stop moisture getting inside the smartphone, as do external buttons, MicroUSB or Lightning ports and 3.5mm headphone jacks.

The charging and headphone ports are also fixed in place in a way that very tightly presses them up against the inside of the frame. 

Another area of vulnerability are speakers. Air and audio need to pass through the speaker to outside of the frame, but the ‘grille’ needs to prevent water from getting in. Typically manufacturers use a screen made from an incredibly tight mesh that prevents water getting through without external force.

That's why smart watches aren't usually water resistant to 50m (not even Samsung's latest offering!). The Apple Watch 2 achieves this amazing feat by using the speakers themselves to eject the water. No doubt other manufacturers will copy this process.

Finally, as a point of interest, the iPhone 6s and iPhone 6s Plus were also designed to be water resistant (although not officially). I've seen inside these and the difference compared to earlier models is obvious. You can read more about this here.

[EDIT]

This edit addresses the issue raised by Adam Rice in their comment, namely that my original answer didn't cover the issue of what prevents water inside the charging port from damaging an iPhone 7 by conducting electricity between its connectors?

As I mentioned in my original answer, rubber seals are used to stop moisture getting inside the smartphone via the sim tray, external buttons, charging ports, etc. However, what I forgot to mention is that these phones have a moisture sensor in the charging port that protects the phone from electrical short circuits and damage. This basically means that the phone will not accept a charge if the sensors detect moisture. Likewise, electrical current between the connectors is also prevented if moisture is detected by the sensors.

Also, it is worth noting that having a water-resistant smartphone is a bit like having a diver's watch that is water-resistant to 200m. The instructions of such watches tell you not to press any of the buttons while it's under water. Likewise, with a smartphone, don't plug it in to a power source while it's under water.