I have read over the OGL and researched copyright laws, but I still am not sure how much of what I want to do is legal. What if someone wanted to write a story about a pre-existing campaign, such as The Temple of Elemental Evil or The Village of Hommlet? If descriptions and specific names were changed enough, would it be legal? How does one go about getting the rights to use material like that, or is it simply infringement?
OK, so here's game intellectual property 101. There are niche exceptions to all of it, but at a high level it's going to hold for 99% of use cases in the free world.
If you don't already know all the stuff below you should not enter into any commercial enterprise based on someone else's IP without professional legal advice.
You are never free to use someone's intellectual property "just because." Unless you have permission/a license to the contrary, use of this sort is well covered by IP (copyright, trademark/trade dress, patent, et al.) law and is illegal. Even if you're not charging for it.
In some cases you can use an RPG's rules in your product, if they have a license that says you can. For example, D&D 3e/3.5/PF and original FATE use the Open Gaming License to allow people to use those rules under the conditions of that contract. Some games have "open" licenses like this (you can use them without specifically signing a contract with the company), others have "closed" (you call them up and negotiate). In either case you are bound by the specific terms of the license and/or relevant IP law. (Note that the terms of the license can override rights you'd have under the law. Mayfair was successfully sued by TSR for claiming compatibility with AD&D, not because that claim is illegal per se, but because they'd signed another agreement with TSR that forbade it). Anyway, specific to your case, the OGL is only about the game rules (the things designated as Open Content, e.g. in the SRD), NOT about the fluff and fiction.
In more limited cases you can use game rules without a license, as game mechanics can not be copyrighted. Their exact expression can, and various proper-name terms can be trademarked or copyrighted, so you need a decent degree of IP savvy to do this. Patents could block this but as far as I know no RPG rules have been patented.
Trade dress law can be used to block use of unique iconography and formatting (like say the Star Wars: Edge of the Empire dice sigils). You also can't format your product to "look like" an existing RPG's product for the same reason.
In some cases, more rarely, you can use setting (or adventure, or novel) material, if they offer an open or closed license that says you can. Except for some folks like Eclipse Phase who use a Creative Commons license, an open license is rare and a closed one is expensive and you need to "be someone," not a random fanfic author, to get one. WotC specifically does not have any open licensing for their fluff/fiction under any D&D edition. So ToEE, Greyhawk, etc. are their IP and they're not sharing. You may not use them for your own published works.
Anything created based on a copyrighted work is called a 'derivative work' and is covered by the copyright of the initial work. So you may not create and publish a story set in the Temple of Elemental Evil without a specific license from WotC. Even specific characters have been found to be covered by copyright (in addition to trademark and trade dress, which is why you can't use Mickey Mouse). Using "Drizzle the dark elf with two tulwars and a pet black tiger" is probably protected (based on how much everyone pays their lawyers).
If you are doing so just as a fanfic or something on your Web site, it is certainly possible that you can do it and they won't do anything about it. WotC has done cease and desists to fan sites but that's usually people making spell cards or otherwise doing things Wizards thinks they should get the money for. However, legally, they certainly could order you to take it down via various means (C&D, DMCA takedown to your ISP, lawsuit). They tend to want people to make content on their websites to spread their product, without ever saying people are allowed to so they retain the legal right to stop whoever they want.
If you are trying to profit from a derivative work, however, you can expect to get hammered legally. You won't get that far because publishers etc. won't touch you if you're doing this of course.
As for "if I change it enough so it is not ToEE..." There is no hard and fast answer. Transformations of a copyrighted work fall under copyright and it's not legal to do without license, but if you transform it enough that no one knows where it came from (or at least it couldn't be proven in court) it would work. However, having asked this question in a public forum, too late! You are looking at Exhibit A for the plaintiff. Plus, I assume the reason to set your book in the ToEE is to leverage knowledge/popularity of the ToEE, and filing the serial numbers off too hard leaves you with little of that relationship.
TL;DR - Can I Write A Novel About The ToEE?
Sure, you can if you're doing it for fun on your Web site; no never if you are looking to publish it.
Talk to a Lawyer
This should always be the answer for copyright questions. That said, what you are asking about is largely known as parody. However the legal protections of parody are a complicated matter and may require a lawsuit to defend. Thus, you should consult a copyright lawyer before attempting to publish any material derived from these campaigns.
To be clear, however, what is protected by copyright is (nominally) stories, while specific names, logos, etc are covered under trademark law. Borrowing ideas (elemental temples, evil cultists, etc) is not inherently a violation of copyright law. However, again, you will likely want to consult a copyright lawyer before publishing anything.
However, as @SevenSidedDie pointed, out the law is a complex beast and subject to heavy interpretation. Attempting to use parody as an excuse for things that aren't parody may get you in a lot of hot water. Here's the example he provided: http://www.nytimes.com/2009/07/02/books/02salinger.html?_r=0
So, for the fourth and final time, consult a copyright lawyer.
"Not only will you not get [your book] looked at, you won't get it published."
R.A. Salvatore, during a Meet the Author speech, stated this and added that not only would it limit you to one specific license that you could sell it to, but you would have to have every citation perfect.
He suggests writing something generic that could fit in to a genre and if a specific license picks it up, then an editor could make it more specific to fit their license. He also states that you would not be able to solicit that writing to anyone other than the license holder.
The answer doesn't appear to be a "no you cannot" but it does appear to be a "no you should not."
Simple answer: probably, if you don't "trade" with the name(s) in a prominent position, but you better be able to pay the court costs to argue with the multinational corporation that thinks it's not okay. If you dug your heels in you'd have a fighting chance, but forget about having a life while dealing with it.
Alternatively, write to their legal department and ask about licensing deals. You never know...
Many people have given the copyright-oriented answer to this question. I'm going to give an OGL-oriented one.
What is the OGL?
Generally, IP laws apply before you can do anything legally with someone else's IP. The OGL is a license for you to use specific parts of a work, as identified by that specific work.
What is covered?
Only products that have the Open Game License attached (usually found at the back of the product) and have declared some of their content as being Open Game Content. (Important note here, most official D&D products by Wizards of the Coast are not covered by the OGL, meaning you can use absolutely nothing from them. In your specific example, the Temple of Elemental Evil is not an OGL product and therefore you cannot use the OGL to do anything with it anyway.)
What can I do with it?
Usually - but not always - it is used specifically to open (allow use of) a number of identified rules from a published work. This means that in most cases, what you're allowed to reuse from an OGL work is the rules, and only those rules that have been specifically identified for reuse. Many OGL publishers simply open "all rules" within their OGL work but you need to check the specifics for every individual piece of work. There should be a declaration (usually near the front, often in a box on the contents or credits page) that informs you what specifically, within that work, is Open Game Content and available for you to reuse (it may be a listing of what is/isn't Open Game Content within that work, or may simply state "all rules within this product are Open Game Content")
Campaign setting information is almost always specifically excluded in that statement (and in fact, the OGL itself excludes it under the Product Identity clause) Normally, this means the rules from that product are fine, but any specific mention of an entity (an NPC, race, country, or almost anything else campaign-specific) is excluded. The grey area here tends to be content such as OGLed monsters and rules pertaining to specific races/entities in the campaign world, as the monster name and non-rules details are not specifically rules, so it is usually best to avoid reusing those unless there's a specific OGL inclusion clause for them in that product's declaration of Open Game Content.
That said, there are some instances of completely-OGL campaign settings, where the publisher is happy for people to expand on the setting rather than just the rules included within. These are relatively rare, however.
If you're in the position of needing to ask questions about what you can and cannot do with the OGL, then as other answers have stated you need to consult a lawyer before releasing anything.
So, generally, the answer is no, you cannot use the OGL as a means to make use of someone else's campaign setting, except in the rare case that they have made allowance for that by identifying the setting itself as Open Game Content in their declaration. Without that declaration, you have no license to use that IP, and are pretty much breaking the same laws you would be had you chosen a non-OGL IP to base your work on (which usually puts you in the realms of making an unauthorized derivative work, and is as you have put it in your question - "simply infringement" unless you have obtained permission from the IP owner(s).)