Ranks are based on the completion time. For the higher grades the game requires a lot more optimized movements and attacks over the course of the level. For instance the gif on this page showcases how an S rank completion looks for the first level, though obviously there is more than one way of getting the same speed.