How I helped a novice DM with this
A lady I work with was starting a D&D group with her church youth group, and discovered that I had long experience with D&D and DMing. She asked a very similar question to yours, in terms of "How do I do this NPC thing? There are so many kinds of characters to play!"
I broke it down into a few parts.
1. You'll get better by doing, don't worry
When you are new to DMing, no matter what someone else tells you, you still have to go out and do this NPC play and as you do it more you'll be more comfortable doing that.
2. Give important NPCs Traits, Bonds, Ideals, and Flaws
I recently noted that some of the published adventures do this now, but back then I hadn't noticed that. I pointed her to Chapter 4 (Player Backgrounds) in the PHB and walked her through how to roll for or hand pick those aspects for an NPC. One of each. These weren't to be a three dimensional character study, they were meant to be a point of departure to help her get a feel for who the most important NPCs are.
3. Play the NPC from one of three opening stances
As each NPC interaction begins, determine if the NPC is generally hostile, neutral, or friendly to the PCs. The DMG has a small section about that that I showed her when I gave her a new copy of the DMG (my 'welcome to the world of DMing' present). She had not seen that and appreciated how it would help when she got stuck.
4. Start by acting naturally and simply, then let the interaction grow organically
As one gets more experience with NPC play, one can get sneaky and more nuanced, but I suggested to her not to make her job hard on herself. Start with a regular conversation, and let it grow from there based on what the PCs do and say.
Result? After about a month and a half I got a box full of homemade brownies and a big "thank you" for helping her get started. She told me that she had found those all of these tips useful.
She's still DMing for that group. Last time we talked, she's near the end of her second campaign a bit over three years later. (I also introduced her to roll20 as a resource when the COVID nearly did for her campaign).
How do I do it?
After all these years, there's no simple answer but some things I typically focus on during an NPC interaction with PCs are:
- What do I(NPC) want?
- What am I(NPC) afraid of?
- Are these PCs helpful, dangerous, or simply unknown?
- If I(NPC) help them get what they want, do I believe they'll help me get what I want?
- What have they done for me(NPC)?
Where do I go when I get stuck? Usually, the Alexandrian
Justin Alexander has an RPG blog with a lot of good DM advice. When I am looking for a better idea, I check out his articles on NPCs.
Here are some of his articles on NPC from the DM perspective:
NPC templates., advanced NPC templates, memorable NPCs, and a couple of others here and there.
"How do I play NPCs" is a pretty big topic. 😊 As The Alexandrian notes in the memorable NPC article:
This will not attempt to be an exhaustive discussion of how to create memorable characters. You could write whole books on the subject, and people have.
Some related Q&A already posted here at RPGSE
Making engaging NPCs, how to show emotions for different NPCs, becoming a better NPC/Monster actor, and creating emotions. (Thanks @MikeQ for digging these up and putting them in a comment under the question).