There are already many fantastic answers here, and I wanted to add a bit about how I incorporate many of these ideas to create NPCs:
Generation tables!
You can find many versions of these online, but I like to make my own (for everything but names. I hate naming things!). Having a good array of usable details for an NPC, especially if you have a variety of types of detail available, makes it easier to create NPCs from scratch at need and also provide as much detail as you may require, without needing to improvise very much.
Basic details include race (human, elf, dwarf, etc.), general appearance (tall, stocky, imposing, kindly, etc.), and a memorable detail (scar, accent, limp, fashionable clothing, and so on). Those are usually enough to get through a conversation the players initiate with a random, unplanned NPC.
If you need more details, either because the players take a liking to the character and require more details, or because you identify one as a good opportunity to advance the plot (or contribute to other DM goals), you can always add elements from additional tables.
You can have a table that covers any conceivable element of your setting and scenario, and then always have those details ready to apply when it's appropriate. In my games, I often like having tables for political affiliations, connections to resources the PCs may want, disposition towards others, and social station. I may add others when a setting calls for them, and pass over some when they seem inappropriate. I also fix certain elements to particular tables, or particular table entries, when appropriate (a random passerby near the tannery is probably not going to be a wealthy person, for example).
The basic idea I use to guide myself in preparing tables is to think about the important details of the setting and the themes of the scenario, then think through what types of detail might be important enough to plan this way and what specific details fit well. For example, in a very political, social game I might include social station and political connection as details, while in a dungeon crawl those are probably going to be less important (if not irrelevant). Your particular needs may even allow you to keep a small set of tables in your mind, without the need to write them down and reference them.
I tend to think of the exercise as a part of worldbuilding, and the effort helps me to plan what sorts of details are appropriate for what I want to express about that world. It improves my ability to improvise, to keep the story on track, and to give more depth to the game overall. The NPC generation is almost just a bonus.