In step 1, the mean function is applied to the auto$mpg vector, grouped according to the auto$cylinders vector. The grouping factor should be of the same length as the input vector, so that each element of the first vector can be associated with a group.
The tapply function creates groups of the first argument based on each element's group affiliation as defined by the second argument, and passes each group to the user-specified function.
Step 2 shows that we can actually group by several factors specified as a list. In this case, tapply applies the function to each unique combination of the specified factors.