Nails
‘Nailing’ something is basically the equivalent of hitting the nail on the head. Hitting the nail on the head is, as anyone who’s ever tried hanging a picture on a wall knows, something that requires great precision and the proper application of force (and in my own case, often also the proper application of a few Band-Aids or similar).
As such, it is quite logical that ‘nailing’ something—i.e., fastening it with a nail by delivering one quick blow in exactly the right place to make it sit tight just where it’s supposed to—would acquire the meaning of “to perform or complete perfectly or impressively”.
Screws
Unlike nails, screws are not quickly fastened with one blow. Rather, they must work their way in slowly, and they do so while turning around constantly.
It is a very common metaphor, cross-linguistically, to indicate that something has gone wrong or is not as it should be by likening it to something that turns around or loops out of place. A screw is a good candidate for this. (Compare also the word awry, meaning ‘amiss, wrong’, which is etymologically from the now obsolete verb wry, which meant ‘to twist, turn, swerve’. That’s a similar development.)
If a nail gives the mental image of something going straight in, according to a linear projection, just the way it’s supposed to, a screw gives the mental image of something curving, looping, winding around, in an inefficient manner.
Further derivations
Once you’ve got those two basic meanings, it’s very easy to derive further slang terms from them. The nail-based ones are actually remarkably few in number, but the screw-based ones abound: you can screw something up (mess it up), you can be screwy (crazy), you can be screwed (ruined, done for), you can ‘screw it’ (forget it, leave it aside), you can screw someone over (cheat them), you can screw around (fool around), you can screw someone (as in, “Screw you!”, not-so-politely telling them to go to hell), etc.
Interestingly, both ‘nail’ and ‘screw’ can refer to sexual intercourse—but with the very fundamental difference (borne over from the basic meanings of the word) that screwing someone just refers, in a roundabout way, to the general ‘in-out’ motions performed during sex, while nailing someone indicates that there is a nailer and a nailee: one party is ‘using’ the nail, and the other party is implicitly likened to a wall that the nail goes into. In other words, it is quite common for a guy to brag to his friends that he ‘nailed’ a girl; but not very common for a girl to say that she ‘nailed’ a guy.
"being drug up on the carpet and then run up the mast"
It's a fine example of mangled idioms. I take it "drug up" is the writer's own way of saying "dragged up". He's probably thinking of "being dragged before the boss", but he's combined that vague thought with the phrase "on the carpet", which is used about people in trouble with their superiors. Not content with that, he progresses to an image of someone being "run up the mast" (picture it if you can). The only things that are actually run up the mast are flags or sails, and the popular phrase is exclusively used about the first. This is likely an exercise in free association, where the writer is thinking of "hanging from the yard-arm", but can't remember the exact words, so he latches onto the first substitute with naval overtones that springs to mind.
Best Answer
OED, in the entry for bad boy, n.,
attests use of the sense as early as 1969:
Prior use of the phrase in the given sense can be assumed; it follows on the more general use of 'bad' in the sense of 'good', attested in the OED entry for bad, adj., n.2, and adv. from 1897:
The 'impressive' sense of 'bad boy', however, describing things, owes a greater debt to its immediate progenitor, the archetypal 'bad boy', attested from 1860 in OED:
These 'bad boys', often ambivalently regarded, have been immortalized in literature: Thomas Bailey Aldritch's 1869 The Story of a Bad Boy; George W. Peck's 1883 Peck's Bad Boy and His Pa, along with its sequels; Mark Twain's 1884 The Adventures of Huckleberry Finn.