Grammatically, a while is a noun phrase in which "a" is an article and "while" functions as a noun meaning "a short period of time"; awhile is an adverb meaning "for a while." In other words, the meaning is the same, but the structure is different: the word awhile has "for" built into its meaning.
The test of which to use is to consider whether "for a while" may be used in the sentence where we intend to place (or have placed) the word awhile – without changing anything else.
> "I'll wait here awhile" is correct because we could also say, "I'll wait here for a while."
> "I'll wait here for awhile" is not correct because we have actually used the word for twice, given that awhile = for a while: "I'll wait here for for a while."
> "I'll be there in awhile" is not correct because we would not say, "I'll be there IN FOR a while."
> "This may take awhile" is not correct because "This may take for a while" is not idiomatic English.
> "My mother is staying awhile" is correct because we could also say, "My mother is staying for a while."
The two-word noun phrase (a while) is probably more often the correct choice than is the one-word adverb (awhile). Certainly, most misuses of a while / awhile involve using awhile where a while is the appropriate construction.