Riot measures what they call "core metrics" so that, in their words, "so that if you're doing the right thing for your team, we're going to score you well for it."
http://boards.na.leagueoflegends.com/en/c/gameplay-balance/bZL8vAWw-champion-mastery-qa?comment=00540000
You get compared to other people in your region who played the same champion as you in the same role as you, the higher your score amongst these people the better your grade will be.
https://support.riotgames.com/hc/en-us/articles/204211284-Champion-Mastery-Guide
Also, there are a lot of people giving false advice, a lot of people believe that kda and cs is somehow a direct factor in your grade. These people are most likely wrong as it has been observed that a player can have a pretty terrible kda and cs and lose the game yet still achieve a S level grade.
http://boards.na.leagueoflegends.com/en/c/gameplay-balance/cTJF3oUc-s-on-feeding-trundle
Riot has also noted that even the best of players cannot consistently get the highest grades (though they do get them more often), which leads me to believe there is at least some degree of luck involved.
The true workings of the system are unlikely to ever be revealed to us, so the best you can do is just doing your best and not worry about it. Good luck.
Sorry, that has become my standard response post to Mastery Rank posts. XP
If what I hear is true, that it takes into account all those details PLUS how well the champion is doing in the meta
I see no way for Riot to realistically update their metrics to keep up to date with any meta that happens in League, how would they even do this? That's not to say that the Meta doesn't affect your chances of getting those high S Grades, but only due to the match ups which can favor certain champions to do better. It's the same with balance changes and other changes that happen in League, if the changes favor your champion then you have a easier time in matches.
But how significant are these factors when it comes to getting those high grades? Hell if I know. Your decision making and that of your opponents is probably a lot more important and impactful. Getting a damage buff on your ability due to balance changes doesn't do as must towards winning or losing a game as face checking a bush with an ambush in it during the late game.
The better you are the better you'll do in game and thus the better the chances of getting a good grade, however also consider the stupidity of your opponents, if your opponents are having a particularly bad day/match and make really stupid mistakes that you managed to capitalize on, you will probably significantly inflate the grade you get for that match. So theres a good argument to be made for a luck factor. The same can be said about bad judgment when deciding team comps.
And then theres the fact that the highest Grades are only given to the very top % of performances. So amazing/weird games had by the player base pile up, any sort of game piles up and can inflate the requirements for the Grades, the longer this goes on the more rare the high Grades become. I have no idea if Riot puts a limit on this stuff or if this is even a real problem, I've heard no word about it.
In the end though we have no real answers, so just don't worry about it, just play and do your best, eventually you'll get good+lucky enough and get the Grades you need for your Mastery.
If you want to take a gamble for high S Grades, consider taking your champion into a role where he isn't normally played. It stands to reason that you'll have a easier time getting high Grades in a unorthodox role due to facing lesser competition. But this is just a guess, we don't know if it really works. It also means you're probably handicapping yourself and your team because the champion won't be suited for the role or you don't usually play the role, so beware of that, your team may hate you. So it could take a while before you get the best performance out of something like this and it is just a idea.