How to set Rights and Permissions using the SDL Tridion core service API

Today I saw an interesting question on the Tridion Stack Exchange site about setting permissions on multiple categories. Of course, a slightly broader question is also interesting – how can I also set multiple permissions on folders, and rights on publications... Read the full text.