How does the garbage collector determine whether an object is garbage? Does it refer to the stack to check the references to the space allocated in the heap?
I'd recommend you to read some literature about this: Java theory and practice: Garbage collection and performance and Garbage Collector Basics and Performance Hints.
It employs a mark and sweep algorithm. The simplified version: It starts out by considering all objects to be eligible for collection. It then scans for roots to objects. Any rooted object are then marked as in use. Following that all remaining objects are considered garbage. There's an excellent description of the details in CLR via C# by Jeffrey Richter.
This article by A. Hunter explains it all pretty good.
In short, the GC follows references to find all unreachable objects. An object refers to another object if it or any of its superclasses contains a field with the other object.
To achieve this, the GC keeps track of four types of root objects called GC roots. The most common root type is all variables created on the stack (as you guessed). Other root types are statics, interop-related and objects with a finalizer method.
So yes, the GC refers to the stack in most cases.