There are plenty of articles on the web explaining garbage collection in general and garbage collection in Java.
This one for example.
Because of this there is no point in repeating it all in a forum post. You should study some of these articles and then come back if you have any more specific questions about what you have read.
In a nutshell, there is a thread that can stop all other thread that will wipe not used objects to free the memory. This is rough speaking, as Joanne said, there are much more content about in this forum and in internet (would not be nice to repeat everytime, better we stand over the shoulders of who already made a good post here our a blog, for example)