To go against the grain means to go against the generally accepted practice, or societal norms, or simply something that is unpleasant. The phrase may be used in a number of different ways, such as in relation to someone else, or describing an action someone does not want to take. The phrase has been in use for centuries, but was widely popularized and set down in writing by William Shakespeare.
The most common modern usage of this phrase is to describe something strongly against societal expectations. For example, one might say, "It went against the grain, letting your students call you by your first name, and now look at the consequences." In this sense, the phrase is almost exclusively used about someone else, and is nearly always used pejoratively.
A slightly different usage might be to describe something that one needs to do, but that goes against one’s own inclinations. For example, someone might say, "I’m not one to go against the grain, but in this situation I had no choice." It is still used in a pejorative sense, but not as an accusation, more as an acceptance of a situation in which all choices are bad.
Going against societal expectations is not necessarily a bad thing, and in many cases would be lauded. In these cases, however, a different idiom would usually be used. For example, they might say, "She always did march to the beat of her own drum." This has much the same meaning, but the connotations can be much more positive.
In some cases, someone might not want to indicate a negative stance by saying that someone went against expectations, but he or she might not want to applaud the action either. In this case, a more neutral idiom would instead be employed. An example of this would be to say, "All roads lead to Rome." This means simply that there are many different ways of doing something, and that none are necessarily better than the other. One might also say, "It takes all kinds," which would similarly express a sort of acceptance of the fact that things are done differently by different people.
Other idioms have a similar meaning to going against the grain. For example, people often speak of going against the current or against the tide to refer to the same opposition to societal norms. Or someone might talk about swimming up stream when going against those norms introduces hardship that needs to be overcome.
The idiom itself comes from the real world, where planing wood against the grain leads to splinters and a surface that isn’t entirely smooth. The easiest way to plane a piece of wood is to do so with the grain, and this also results in the best final product. So planing would the other way indicates taking an action that is not only difficult, but also leads to a less desirable final product.
Shakespeare first wrote the phrase down in his 1608 play Coriolanus. In it, the character of Sicinius, speaking with Brutus, says "Say, you chose him; More after our commandment than as guided; By your own true affections, and that your minds,; Preoccupied with what you rather must do; Than what you should, made you against the grain; To voice him consul: lay the fault on us."