The difference between the two for tangible objects is mostly straightforward and has been addressed by the other answers. Briefly, use dasselbe for literally identical objects and das Gleiche for objects which are only very similar.
For abstract concepts, the main problem is that for any two concepts which are somewhat similar but not identical (and thus would only be gleich at first glance), there always exists a more abstract concept which contains both of these concepts and would thus justify using dasselbe for them. For example, two different points of view on the same thing would be:
Hans befürchtet, dass er im Regen nass wird.
Paul befürchtet, dass er im Regen nass wird.
Hans und Paul befürchten das Gleiche.
Hans befürchtet, im Regen nass zu werden.
Paul befürchtet, im Regen nass zu werden.
Hans und Paul befürchten dasselbe.
In the first point of view, the things the two are afraid of are [Hans wird im Regen nass] and [Paul wird im Regen nass] and thus slightly different. In the second point of view, both are afraid of [nass Werden im Regen] and thus of the identical abstract thing.
This problem also exists the other way round: If you are referring to one abstract concept in two cases (and thus would use dasselbe at first glance), there often exist two similar but not identical less abstract concepts for each of the cases which you could be referring to as well (and thus would justify using only das Gleiche). For example, take the above example the other way round.
To make things even more confusing, there exists a grey area between abstract and concrete. For example when ordering meals in a restaurant:
Hans: Ich hätte gerne die Heringspizza.
Paul: Ich hätte gerne dasselbe / das Gleiche.
The arguably predominant interpretation here is that dasselbe is only correct if Paul and Hans really want to share one pizza. However, one can also hold the point of view that they are not referring to the concrete pizza they will be having but to the abstract item on the menu and thus dasselbe would also be appropriate if they are not sharing a pizza.
Due to the above you can usually find a justification for either choice in many situations and most people won’t be confused no matter what you choose.
However, in the rare case, that you cannot find two less abstract entities to refer to, I would prefer dasselbe:
Wir gehen in dieselbe Klasse.
Also, in cases where there are two predominant levels of congruence for the abstract entities you are referring to, I suggest using dasselbe for the higher level of congruence. To take one of your examples:
Er sagt in jeder Vorlesung dasselbe.
Er sagt in jeder Vorlesung das Gleiche.
I would use das Gleiche if he is saying the same thing contentwise but not with the same words, e.g. “alle Studenten sind doof” in one lecture and “alle Studenten sind dämlich” in another one. Only, if he is using the very same words in every lecture, I would use dasselbe.