How do I switch off/shutdown an iPhone with a broken power button?

Assuming you're on iOS 11: the easiest way is to go to Settings > General. Swipe up until you see the Shut Down option at bottom.Tapping on Shut Down will mimic using the power button.

If you're not on iOS 11: then you can use Assistive Touch: go to Settings > General > Accessibility and switch on Assistive Touch. Now you'll see a bubble appear on the screen. You can move this around, but most importantly you can tap on it and select Device and then select Lock Screen. Doing this will give you the option to switch off your iPhone. (Note: You can do this with iOS 11 as well).

Switching it back on

To switch it back on you will have to connect your iPhone to your Mac/PC or to a charger with the Lightning cable. After a while it will power up and you'll be able to unlock it.