How to prevent batteries leaking out in devices?
Acid cell and Alkaline Batteries function by a process of controlled corrosion of two different elements which creates a voltage potential and a current if the circuit is completed.

This process is in continuous motion from the day of manufacture, while it sits on the shelf in its packaging, while installed in the equipment during off/idle periods. It accelerates with higher discharge rate during use, and continues after the cell is unable to produce useful power to run the device

Alkaline batteries have a sealed container that serves as one of the electrodes. While it undergoes a lot slower corrosion than the case on the old Acid cells, it still corrodes. It is part of the process that allows the battery to function. You don't prevent it.


Dead batteries should never be left in equipment, their process is nearly complete and the shell is close to breach.
Live batteries have a date on them for a reason. Chemical reaction occurs even if the battery is live and able to run the equipment. Often with today's LED technology, for example, the batteries will corrode through and start leaking long before they become too weak to run the flashlight.
Prevention is worth a pound of cure. Remove batteries from the device when not in use for extended periods of time. If it is part of a crash kit where you need batteries with the device at all times for emergency use, store them in a separate ziplock bag and check their dates periodically as well as for leakage.
Don't mix different cells in a device which takes more then one cell. Alkaline cells tend to leak when a potential voltage is applied, i.e. when you try to charge them. This can happen in devices with more than one cell in series if some of the cells have less capacity than others. This is why one should never mix cells by type, brand or age in devices where the cells are connected in series (which is virtually all devices that take cells in the same compartment).