The Meaning of On-Spec Writing
Most writers and editors know that when they approach a publication with an on-spec article they have written, it may not be immediately accepted. Simply put, it means that you are writing something for a publication without the guarantee—implied or explicit—that the publication is going to buy the work from you once you've finished writing. Basically, as a writer, you're speculating that someone will give you a chance and you're taking a shot in the dark. The term "on-spec" to those in the writing profession means to speculate or gamble on booking a gig.
On-Spec Fiction Writing
Writing on spec is so much the standard in fiction writing that very few writers today even think about the fact that is what they're doing. If an editor doesn't buy the piece, then that's a big disappointment for you, the writer. However, as a writer, you never take no for an answer and do what every other fiction writer does and stuff your story into another envelope and send it off to the next editor in line.
You'll keep repeating the process until either the story gets bought, or you run out of editors to approach. It is relatively rare that a fiction story is so specific to a market that it couldn't be sold somewhere else, especially if you're willing to do a little touch-up work in between attempts to get published.
Non-fiction writing is a whole other ballgame because the writing is often very specific to a particular market. As a hypothetical example, let's say you're writing a piece for Exotic Bug Quarterly—the magazine by and for exotic bug enthusiasts. There is a somewhat smaller secondary market for the piece you are producing. Of course, you could put a "generalist" or "populist" spin on it and widen your market to the garden pest, tropical forest, or bug collector market, but you are still talking about a limited use article.
Therefore, writing on spec non-fiction in some markets is a riskier proposition. It is why the submission process for most non-fiction markets is designed to reduce the risk for both writers and editors.
Non-fiction writers begin the submission process with a query explaining their story idea and any previously published story clips that are in sync with the proposed story. The editor looks at the story ideas and the clips and tries to determine whether or not you're a good fit for the story. If the editor deems you worthy, you'll be contracted to write the story for an agreed-upon fee. If you're contracted to write the story, and the piece is found unacceptable or is not run for some other reason then most likely you'll receive something called a "kill fee." Kill fees are payments sent to a writer and will usually be only a percentage of the agreed-upon price.
Until you get to a certain point in your freelance career where editors are pre-emptively asking you for work, everything—or at the very least short fiction—is assumed to be written on spec.
With non-fiction freelance writing, it's a matter of judging the risks and rewards. If the rewards make sense for you, as a writer, it might be worth the risk of putting together a piece you might be able to sell. Whatever you do, make sure you understand clearly what you are doing and why, and what the upsides and downsides are for you.
Also, it goes without saying that before submitting any work to a publication you should know the kinds of work they publish. Only submit work that is on target to the publication and follows their voice and style as much as possible.