Some people suggest to just record everything as hot as possible, as close to the max as possible, but that might not be the best procedure. Some possible issues: you risk ruining the take by clipping the signal. Some devices induce more noise when working at their limits You have to predict the dynamics of the signal in advance or process it with limiters or compressors, just to name a few.
In digital recording one important factor is the bit depth.
In the average home studio, recording at 24 bits, you want your signal to average -18dBFS. Peaks can be around -10dBFS. At 32 bits the that range is lower, at 16 it is higher. This leaves more than enough room for your signal to have a large dynamic range, while staying far enough from noise.
From SOS article How much headroom should I leave with 24-bit recording?
The basic idea is to treat ‑18dBFS as the equivalent of the 0VU mark on an analogue system's meter, and that's where the average signal level should hover most of the time. Peaks can be way over that, of course, typically kicking up to around ‑10dBFS or so. Drums, being largely transient peaks, will be kicking up there regularly.
If the material you are recording is well controlled and predictable in terms of its peak levels — like hardware synths tend to be, for example — you could legitimately reduce the headroom safety margin if you really want to. But in practice there is little point.
The only advantage to recording with less headroom is to maximize the recording system's signal‑noise ratio, but there's no point if the source's signal‑noise ratio is significantly worse than the recording system's, and it will tend to be that way with most analogue synth signals, or any acoustic instrument recorded with a mic in a normal acoustic space. The analogue electronic noise floor or the acoustic ambience will completely swamp the digital recording system's noise floor anyway.
Recording 'hot', therefore, won't improve the actual noise performance at all, and will just make it harder to mix against other tracks recorded with a more reasonable amount of headroom. One issue that comes up a lot is the confusion between commercially released media (CD, MP3, for example), which have no headroom margin at all (they peak to 0dBFS), and the requirement for a headroom margin when tracking and mixing.
From article Proper Audio Recording Levels
Stop recording so hot. Instead of trying to get your tracks to peak at -2dBFS, have them peak between -20 and -12dBFS and your recordings will almost undoubtedly sound better. Mixing will be easier. EQ will be more effective. Compression will be smoother, more manageable and predictable. You're in the age of 24-bit digital recording - Relax and enjoy the headroom. Even if your only concern is the volume of the finished product (which would be a shame, but it happens), recordings made with a good amount of headrom are almost undoubtedly better suited to handle the "abuse" of excessive dynamics control. QUIETER recordings have more potential to be LOUD later. It's because they're usually better sounding recordings in the first place.
If microphones are involved, other factor is the ambient noise. If you are not recording in an acoustically isolated room, chances are that your room has plenty of ambient noise. The hotter the signal enters the mic, the larger the signal to noise ratio will be. So, to reduce the noise, the singer can sing louder, the amp can be turned up louder, the drummer can play louder, you can get the mics closer to the source. Just be careful, mics can be damaged if the sound is too loud. Check your mic's max SPL and make sure you are not near that limit. Note that your recording levels in the DAW should still be around -18dBFS at 24 bits.
Other factor is the noise of the devices that you are using. Some devices (pedals, amps, acoustic instruments, whatever) behave differently at different amps. Some will induce some noise and/or distortion at high amps. If you don't want that noise or distortion, the correct gain level here is before the noise and distortion kick in.
If this is too technical and cryptic for you, just watch your input meter when recording and make sure it peaks at around -10dBFS and it stays around -18dBFS and you will be fine. You can even go a little hotter if you want. Recording at max levels is risky and not necessary at 24 bits (make sure you are recording at 24 bits at least).