Quick disclaimer: I'm not a doctor or medical professional. These are simply my observations after pulling several all-nighters in my life.
1. Have a Reason
Not only are all-nighters terrible short-term (the effects are similar to alcohol intoxication), but research shows they terrible long-term too! If you're willing to do this to your body at least have a pressing reason, or have serious motivation to stay awake. Otherwise, chances are you'll call it quits at 4am and destroy your sleep schedule, or even worse, take a short nap the next day that ends up lasting the entire day.
2. Use Caffeine Sparingly
Space out your caffeine usage, and take it in small doses. In other words, only drink tea and coffee, which have relatively low amounts of caffeine. Energy drinks are risky, as they can induce anxiety and cause energy crashes later on in the night. Caffeine intake depends on your weight and age, but I like to drink my first cup of coffee around 11:30pm, and then have my next around 5:30am. Never drink more than one cup at a time.
3. Take Breaks
Take a break at given intervals, or whenever your attention span significantly drops. If you're in the city, go for a walk. At minimum, step outside for some fresh air and take a break from your screen. Breaks will refresh you keep you awake.
4. Eat Wisely
Never eat a large meal during an all-nighter, no matter how hungry you feel. The combination of digesting a large amount of food and 0 sleep is a surefire way to get knocked out. Instead, eat small (non-greasy) snacks.
4. "Trick" Your Body Into Thinking The Next Day Has Started
At 8-9am, or whenever you normally wake up, trick your body into thinking you slept the night, and are now waking up by taking a shower, getting ready as you normally would, and getting breakfast. This will leave your body less confused as to why you haven't slept.
5. Prepare for the Worst Hours
From experience, the worst hours of an all-nighter are from 3-4am and 10am-11am the next day. From 3-4am, your body will be begging you to sleep. To override this, get up and move during this time, and ensure the lights in your study space are plenty bright. The next morning, from 10-11am, you will feel the urge to take a nap. Ignore your body, and stay awake. By taking a nap, as stated earlier, you are risking falling asleep for a prolonged period of time (which will ruin your sleep schedule). Instead, get some sunlight, and/or drink some caffeine.
6. Survive the Next Day
As mentioned earlier, an all-nighter will reduce your motor skills and critical thinking. In other words, don't pull an all-nighter before an exam or presentation. Don't expect to get much work done the next day, and if you must do work, only complete busywork or somewhat easy tasks. As for caffeine, only take it when you are dangerously close to dosing off, to avoid a caffeine crash.
7. The End
To complete an all-nighter, and not suffer the consequences, sleep at a good time. I use the rule of thumb of 4-5 hours before your regular bed time. If you normally go to bed at 1am, go to bed at 8-9pm. Set an alarm for 1-2 hours after your normal wakeup time. NEVER go to bed late on the day of an all-nighter, as this is another surefire way to ruin your sleep schedule long-term. NEVER pull back-to-back all-nighters, as this will not only make staying awake for all-nighter #2 incredibly difficult, but snowballs the effects for the day after.