TIL that appendChild will take existing nodes out of their existing spot in a document (as well as the more obvious action of appending them to another node). :)
This is a good start, but it's limited to "move to front." If you wanted to move something between two intermediate planes, you'd have to reshuffle the whole stack.
Then you can change the z-order by moving the element to the appropriate group. Elements are rendered in the order of an inorder traversal which is why this works.
There's also insertBefore, which you can use to move a node anywhere:
parentNode.appendChild(node); // move to top
parentNode.insertBefore(node, parentNode.firstChild); // move to the bottom
parentNode.insertBefore(node, someNode); // move just below someNode
parentNode.insertBefore(node, someNode.nextSibling); // move just above someNode