It's complicated, but you should already have done a pro forma follow-up
If it's obvious why you weren't a perfect fit, just move on.
If it's a real company (you know who they are, perhaps you had an interview) then you may well learn something by asking why, although really you will usually already know.
If it's a government position or a defense contractor or otherwise a highly structured giant organization, I'm not sure I would bother. The problem is that they probably knew from day one who they were going to hire and were simply required by policy to post the job.
You see this all the time in the defense and government sales racket. The agency you are selling to (and you are selling yourself) is required to offer the opportunity on the open market and to take "bids" to get the taxpayers the best deal.
Of course, this doesn't work in practice at all. I have written elsewhere on Stack Overflow about the Principal-Agent problem. In general, when a government job or purchase is advertised it's purely a fake so that they appear to be following their legal requirement without any intention whatsoever of doing so. A related situation exists with Craigslist where a big percentage of the jobs are, for some reason, bogus. (Recruiters? Email harvesters? Beats me...the formula is usually a too-good-to-be-true posting and an anonymized email address.)
Otherwise, I would say that you should already have followed up even before learning the outcome. After an interview, immediately send an email follow-up and that evening send a written one. (Buy some invitation-style cards.)