First open the Measure app on your iPhone. You will notice two options at the bottom of the screen: Measure and Level. Tap on Level if it's not already selected. You can then use your iPhone in portrait or landscape mode as a Level. The screen will turn from black to green when being held in a level position.
The Measure app was not available prior to iOS 12. Users without the Measure app should launch the Compass app on an iPhone or iPad and calibrate it (if necessary). Then swipe to the left on the screen to reveal a hidden level option. You can then turn the iPhone on its side or on its back to use the leveling functionality.