The short version is they do a search through databases starting with patent databases. They aren't specificLlyspecifically looking for previous patents. If your claimed invention is already described in a patent, a patent application, a journal article, or anywhere else you shouldn't be able to get a patent.
Searching is a art. It is done by text searching and by classification. The patent databases are indexable by class and subclass to a much finer level than "physics".