Sayyiduna Abdullah bin Mas'ud (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings be upon him) said: "Let those who are wise and understanding stand close to me, then those who are next to them in rank, and then those who are next to them in rank. Do not stand scattered here and there, for this will cause discord in your hearts. Also, refrain from the ways of the marketplace."
Hadith Referenceسنن دارمي / من كتاب الصللاة / 1302
From this hadith, it is established that those who possess intellect, understanding, and are educated should stand near the imam, so that in case of necessity they can deputize for the imam, and if there is any mistake or forgetfulness, they can rectify it. After them, those of average rank should stand, and then the rest of the people.
It should be noted that this ruling is not restricted only to prayer, but in every gathering, people of knowledge and virtue should be honored.