As long as the player tries to hit the ball, it doesn't matter whether the player actually hits it or not. Intent is the key.
So when a player takes a swing with the intent of hitting the ball, it's a stroke even if the player whiffs.
There are circumstances in which a swing does not result in a stroke. For example, if a player takes his or her backswing, but stops the club before it would have made contact with the ball, it is not a stroke.
The definition of "stroke" from the USGA Rules of Golf is this:
"A 'stroke' is the forward movement of the club made with the intention of fairly striking at and moving the ball, but if a player checks his downswing voluntarily before the clubhead reaches the ball he is deemed not to have made a stroke."

