Choosing between "a while" and "awhile" can be tricky, because there are times when "a while" is clearly correct, when "awhile" is clearly correct, and then there is a gray area .... Let's try to untangle them, with some examples from Stargate: SG-1.
According to the Oxford American Dictionary:
While is a noun meaning "a period of time."
"A while" can be replaced in your sentence with the appropriate substitute noun--"a few minutes," "an hour," "two or three weeks"--and make sense. Following a preposition, you always want to use the noun.
Jack eyed the packed conference room and asked Daniel, "Will you be okay on your own for a while?" (Or "Will you be okay on your own for a few minutes?"--object of the preposition for)
"Daniel Jackson! I haven't seen you in a while!" exclaimed a bear of a man. (Or, "Daniel! I haven't seen you in several years!"--object of the preposition in)
Daniel grimaced at his least favorite undergraduate professor. "I left academia a while ago." (Or, "I left academia a few years ago.")
Awhile is an adverb meaning "for a short time."
The preposition is built in, so "awhile" can always be replaced in your sentence with "for a short time," which makes it clear that it's never correct following a preposition, or when you want the noun form:
"Will you be okay on your own for for a short time?"
"Daniel! I haven't seen you in for a short time!"
"I left academia for a short time ago."
See how that doesn't work? But when you need the adverbial form, "awhile" is the word for you:
Daniel hooked his thumb towards where Jack was at the bar. "I'll, uh, just be over there awhile." ("I'll just be over there for a short time.")
Jack had thought he'd be waiting awhile before Daniel showed, but Daniel popped up at the bar almost right away. (Jack had thought he'd be waiting for a short time before Daniel showed.)
The tricky bit, as pointed out in The American Heritage Book of English Usage, is that sometimes a noun phrase can act as an adverb.
Jack had thought he'd be waiting longer before Daniel showed. ("Longer" is an adverb that parallels the use of the adverb "awhile.")
Jack had thought he'd be waiting an hour before Daniel showed. ("An hour" is a noun phrase acting as an adverb, which would parallel the use of the noun phrase "a while." "For an hour" or "for a while" could be used here, but skipping the "for" is also correct.)
"A while" as an adverbial noun phrase is the origin of word "awhile," so personally I prefer to use "awhile" any time "for a while" would work. Still you won't be incorrect if you use "a while" as an adverb.
"A while" is always correct. You must use "a while" when using a preposition, or whenever you definitely need the noun form. You may use it as an adverbial phrase.
"Awhile" is only correct when used as an adverb--a substitute for "for a short time." "For awhile" or "in awhile" is always wrong.