Context
During the Cold war, as the Soviet Union spread its ideas of communism to eastern Europe, fear circulated about its popularity reaching the US. There was also fear that Soviet spies were stealing atomic secrets from the US. It should be noted that communist agents did exist in the US government at the time. Most notably, Alger Hiss and Julius and Ethel Rosenberg were guilty of espionage for the Soviet Union (though Hiss was only convicted of perjury).
Rather than sincerely addressing this problem, McCarthy took advantage of it to gain notoriety.