When I step into an aircraft lavatory, the light is off and there is an indicator light saying to lock the door. Once I do lock the door, the light turns on, and otherwise, the light remains off. Why is it that I must lock the door in order to turn on the light inside the lavatory?
Three reasons for this:
The main reason: Passenger comfort. If the light continuously remains on, then if a passenger opens the door of the lavatory when the cabin lights are off, it will fill the cabin with unwanted light. This can be avoided by making sure that the door is closed before turning on the light. Aircraft lavatory doors and door frames are designed to make sure that light coming from inside the lavatory does not reach the passenger cabin and vice versa.
The secondary reason: Energy conservation. By closing the door, though the system is sure that no unwanted light will pass into the cabin, by forcing the passenger to lock the door before turning on the light, the system saves energy by not turning on the light whenever there is no passenger inside. Certain aircraft, but not all, have a little low-energy light to make sure that the passenger can still see inside (and thus be able to see the switch to lock the door) if the door is closed but not locked, but this is immediately switched off the moment the door is opened.
The tertiary reason (obvious): Privacy. The light also acts as an enticement (in case the "Please Lock Door" indicator fails to do so) in order to get the passenger inside to lock the door (because not locking the door will make the lavatory appear vacant to another passenger on the outside).
However, in cases where there are windows in the restrooms (is the case with certain A380s and B787s), sunlight from the outside could potentially enter the cabin when the door is opened. This can be avoided by using an electronic sunshade or by using a Boeing 787-style opacity setting that immediately darkens when the door is unlocked.
UPDATE: The above only appears to be the case on long-haul, wide-body aircraft (I last tested this on a Boeing 777) and not on narrow-body aircraft (the light only slightly brightened when the door, even with the cabin lights fully off, is locked when I tested this on both a Boeing 737-800 and Airbus A320 recently).