If the pitcher's lead is preserved by the bullpen, he is credited with a win. Any time a pitcher enters the game with a lead or a tie (including the beginning of the game) and leaves with a deficit, he is on the hook for a loss (regardless of how many innings he pitched).
In addition, a reliever can record a win as long as he enters the game with a tie or a deficit and leaves with the lead. However, in this case, any baserunner he leaves on will count against him if he scores after the pitcher leaves the game. For instance, if a reliever comes in with a 1-run lead, and leaves the game after giving up a single, he is on the hook for that runner. If the runner scores, he does not get a decision. If the runner is stranded, and the lead is preserved until the end of the game, the relief pitcher will record the win. This is also the case for the starting pitcher if he pitches the requisite amount of innings.
If the starting pitcher pitched less than five innings, but leaves the game with a lead that the bullpen maintains to the finish of the game, the scorer awards the win to the relief pitcher judged to have been the most effective.
Since wins are largely dependent on the offense behind the pitcher and how well the bullpen can preserve a lead, as a stat they are not very useful to judge a pitcher's production. Stats such as Earned run average (ERA), ERA+ (ERA adjusted for park and league factors), Walks plus hits per inning pitched (WHIP) and ratio of ground balls to fly balls (GB:FB) are better indicators of a pitcher's production.