This is a frequent topic/question. The short answer is that there is no right answer. There are so many unknowns about lightning and very few knowns about when and where it will strike. Many sailors have reported lightning strikes near their boats in the water but not hitting the 30+ foot tall mast of their boat... why? Who knows...
Some say having a grounded boat is safer, but increases the chances of getting struck... some say if you aren't grounded your chances are less to get struck.
Some say if the rig gets struck the rigging will create a faraday cage and everyone inside of this should be safe.
Common practices that I find to be in line with my own are:
Check weather often where there is a chance of storms, I have radar on my phone for this ($3/month I think)
Seek shelter prior to a storm hitting if possible
If severe weather is imminent I normally anchor in sheltered area until it's passed when possible
While there is lightning, avoid the bulkheads, touching metal, and electrical systems in the even that the boat is struck.
Lightning can reach 25 miles. There is little rhymn or reason for where or when it strikes. It's possible for you to have clear skies above you and get struck from a significant distance. Lightning is a real killer and this year there have been (I believe) higher than average number of lightning related deaths. I have respect and fear of lightning, so I err on the side of caution as much as I can.
People on this board have been in their boats when they were struck and in most cases it seems the results varied. Some heard the thunder, some didn't, most saw a bright flash, some got sick, some just had some of their instraments fried, some said they had tiny little holes burnt through the gel coat.
My boat has been struck by lightning while in a slip prior to me owning it. Only thing I know that happened with ill effects was the VHF was fried. The hose under the sink in the head had a big hole in it and looked almost like it was melted when I bought the boat and I replaced it. I have recently been thinking that the lightning could have traveled through the hose that is reinforced by metal inside and fried it (I don't know though as rubber is an insulator and metal is conductor... not idea), when I went to replace the hose, the through hull disintegrated in my hands so I don't know if this was age or again degredation due to the lightning strike.
Play it safe and don't put yourself in dangerous situations if avoidable.
Jon