In the law of scotland, a "clerk" is usually a civil servant who helps to run the court. Advocates (barristers) also have clerks, who do administrative work for them.
The main such official in a sheriff court is the Sheriff Clerk. The High Court of Justiciary and Court of Session - which are the Supreme Courts - share a Principal Clerk. In either case, there will be other officials under them, who might or might not also have titles including the word "clerk". All of the administrative work of the court is somehow done by these people, and some roles will entail more legal knowledge than others. The person who is marshalling proceedings in the actual courtroom will be a clerk. Some court orders, mainly procedural in nature, can be signed by a clerk on behalf of a judge.
Sometimes, there are "law clerks" who support the judges themselves, by preparing summaries of upcoming cases, carrying out background research, taking notes at oral proceedings, assisting with assembling the final opinion text, and so on. Currently, there are not very many of these. The Supreme Courts have essentially a pool system, and there are specific clerks who work to the two most senior judges.
In a Justice of the Peace court, where there are lay magistrates, there will be someone helping them with matters of law. This person is not called a clerk, but a "legal adviser".
Additionally, the second most senior judge in the country is the Lord Justice Clerk. The name is because this office originated as a learned advisor, a "clerk of justiciary", whose legal assistance to the judges became so important that the role was eventually elevated to the bench anyway. (In the Middle Ages, a "justiciar" was a royal official who was not only the king's delegate for general administration, but had jurisdiction as a judge over certain public offences such as murder. Some were appointed and in other cases it was hereditary, so having a legal expert assistant was important. The modern successor is the High Court of Justiciary, hence its name.) The administrative side was split off in the nineteenth century, to the aforementioned Principal Clerk, leaving the LJC as solely a judge, but retaining the title due to historical inertia.
There are a few other offices that include the word, like Lord Clerk Register, which nowadays has few responsibilities but was formerly an important job of being in charge of all public records.
These all basically originate in the word "clerk", or Latin clericus. The rough sense is "a literate person who acts as an assistant for somebody else". There was considerable historical overlap between being able to read and write, and being an ordained member of the clergy. The term "clerk in holy orders" is still used in Anglican ecclesiastical law to mean an ordained person (deacon, priest or bishop).