Sometimes it's random. The main character in Solomon's Grave is Nathan Dinneck. Dinneck: because I'd just finished a zombie story before I'd begun the book, and the character's name was Dinneck (no first name). I decided it was original enough I wanted to use it again. His first name was originally Marcus in early drafts, partly because I wanted a name beginning with a consonant, and I wanted it two syllables. I have no idea why, but I don't question my thought process - plenty of others do that for me. I changed Marcus to Nathan because there was another character with an "M" name and it caused confusion to one of my proof-readers.
In my as-yet-unpublished novel, Margaret's Ark, the main character's first name came about when I began the original short story, thirteen years ago, on which the novel was based. We were in the process of buying property (on which our house now stands), and dealing with a very nice woman named Margaret who would later become our neighbor. I used her first name for the character. Come novel-writing time, I needed a last name. That evening I happened to visit my friend Fran Bellerive who lives near a store called Charboneau Shoes. I liked the look of the name, so dropped the 'H' and Margaret Carboneau was born... or at least named.
As I began writing Plague of Darkness, I needed a better concept of the main character, a teenage girl with an attitude - my own teenage-daughter-with-an-attitude was too young back then to serve as a role model
Sometimes it's just a random - I might use any old name as long as it doesn't start with the letters M, Q or T (because it's never a good idea to have main characters begin with the same letter, too visually confusing for the reader). I don't care what name I use. Or an interesting name occurs to me as I write and it drops into the story and I think very little as to why I used it.
There are some names which I have never used, because they are too strongly associated with people in my life. I've tried a couple of times to use the my wife Janet's name, but it's hard to disassociate it from the real person - and you have to be able to do that (unless you're writing a memoir). I've used my kids names now and then, mostly because they keep asking me but only for minor characters because let's face it, I write horror. Sometimes this comes in handy - I might give specific names to victims in my writing, especially ones that get smeared under a slow moving steamroller or something equally as enthralling. I'd planned on mentioning some sample names here, but decided against it - you know who you are. Some names, even of those closest to me, are common enough I can probably manage it, like Joe (my Dad's and brother's name), but not Marilyn (Mom's name, and too unique..).
In the end, whichever name I use, once the book / story is finished they've become so ingrained in my mind with the character that I can't imagine using any other.