Sealing Ceiling holes
For small nail holes, use spackle. This is the simplest, cheapest way to fill small holes that aren't subject to significant movement.

For cracks/seams, such as at corners or around window trim, use caulk (probably a paintable latex or latex+silicone caulk, as they're easy to work with).

For deeper holes/cavities, such as around a pipe that is going through your floor, use expanding spray foam. This product will expand around whatever stuff is there, providing a good seal to block insects. But beware that the expansion can come with a lot of force, so don't overfill; if you're working in tight places, the expansion can cause damage. Around windows and doors especially use a minimum-expansion foam.

After patching the hole, make sure to seal the repair with primer before you paint it.