. A persistent theory is that the British Army's 99th Regiment of Foot were renowned for their smartness, so much so that the other regiments were constantly trying to emulate them - to equal "the nines". Unfortunately, the story dates from the 1850s, and the phrase is older.
Other attempts at explanation connect it with the nine Muses, the mystic number nine, or perhaps reaching a standard of nine on a scale of one to ten - not perfect, but doing very well. Some think the phrase dates from the 18th century, first reported in 1787 in the form 'Twad please me to the nine.' The dressed is a later addition.
Another explanation which relates more to clothing is that this saying refers to the amount of fabric required to make a quality suit - nine yards.
Some speculate it's a reference to the nine Muses of Greek mythology.
Walter Skeat (the editor of the Oxford Etymological Dictionary and the first secretary of the English Dialect Society) once proposed that it could originally have been "dressed to the eyes", which in medireview English would have been "to then eyne"; the phrase could afterwards have mutated by the same principle that caused "a norange" to change to "an orange". But the reverse problem of dating arises here, in that if it were truly medireview in origin one would expect examples to have turned up before Burns' time. So it would appear, at least at present, to be a bit of a mystery.