I was going to say, I'm sure there was a snopes page about this very saying, and there is (sometimes when I'm sure about pages I've seen in the past, I'm either wrong, or the page has disappeared for some reason):

Claim: Barkeepers kept track of patrons’ tabs by chalking p’s and q’s on the wall (for pints and quarts), hence the admonishment to “mind your p’s and q’s.”

Status: FALSE

Sorry, AliBaba, but as one of the few people who's both still posting and has been here longer than me, I think you might deserve to have it drawn to your attention...!

Sadly, there is no accepted theory for where that saying came from, but as I understand it one of the more likely ones is to do with printing, and the fact that "p" and "q" each look like the other when reversed and so were difficult for printers, in the days of typesetting by hand, to distinguish. (This is also mentioned on the page above - it's not definite either, but it seems from other things I've read to be more likely than the others. Also pretty much what GenYus said.)

(eta) In practice I speculate that it's probably a mixture of the difficulty of distinguishing "p's and q's" (in the handwriting sense rather than printing) and what UEL said - and this also matches the sense in which it's used, to tell people to behave; it works as an admonishment to children in both senses - handwriting, and "mind your manners", and it's a pune or play on wordes that people would remember.