This is a strange question, but the literal answer is that email servic provides like Google, Yahoo and so on would implement this differently, depending on how they internal store mail messages and folders. For example, if email messages are stored as individual files and folders are represented as directories, then moving an email to the spam folder would be done as a file rename / move. On the other hand, if mail is stored in an SQL database, moving a message from one folder to another would be an UPDATE to a row in (say) a mail descriptor TABLE.
There are many possible ways to represent email messages and folders, each email service provider is likely to do it differently, and we have no way of knowing how they do it.
I would hesitate to call this process an "algorithm". Certainly, there will be no single algorithm, given that representations vary, and that models of what a folder is vary.
I don't see any connection between your question and the "java" or "javamail" tags. The chances are the big providers don't implement their email services in Java.