I have a Nexus 5. Even if I enable security passcode or pattern, someone can still switch off my mobile using the power button. How can I prevent it?
You cannot (barring any hardware tweaks.)
If you root your phone and install a custom ROM, you may be able to remove the "Turn off" and "Reboot" options from the Power menu. However, Nexus 5 (as do most modern phones) has a hard-wired feature, where holding the power button for 10 seconds shuts the phone off. This is done so that you can restart the phone if it freezes up. Since Nexus 5 doesn't have a removable battery, this hard reset would be the only way to shut it off without having to wait for the battery to drain.
If you are technically inclined, you can always make hardware modifications to the phone which would prevent the Power button from functioning normally. However, such mods are not for the novices, and will certainly void your warranty.
You could make/modify a phone case so that the power button is covered. This would avoid having to void the warranty.
So I would say the best way would be :
1 - custom rom to alter the turn off menu
2 - not to remove the power off option but replace by a fake one that would pretend that the phone switch off (playing a dumb animation as usual) but just putting the phone in sleep/locked mode, actually it should already start at that point to turn on gps, send position thru email, record voice and take picture... only a thief would use a button that a user dismissed.
Aside that a device should not have a lock screen, otherwise a thief would flash it straight away imho, he should be able to use it as he wants, or supposedly as he wants... probably a fake normal mode without lock activated by the fake turning off described in 2-
You can try this app to achieve this - Smart Lockscreen Protector
This app blocks the power menu from showing up if your phone is locked. So it stops people from powering off or rebooting your phone when its locked.
Note: However if the person tries to hard reboot by holding the power button for more than 10 seconds (hard reset), then this app cannot support that. As it's hard-wired to do so.
It at least protects your phone to some extent, without rooting your phone.
There is no way you can prevent the phone from getting turned off(without rooting, which will void your warranty if the device is in warranty).
If your phone is supposed to get stolen, it will be.
So Only thing you can do is keep your devices safe.
Just root your phone and buy Cerberus. They got this option.