"magic" here refers to "magic numbers": a special value that's in a known place in a file that identifies its type. The
file command has a database of these numbers and what type they correspond to. The library that goes with that database is called libmagic, and you can access that from your own programs.
They aren't necessarily "numbers" as we might think of them. For example, a PNG image file always starts with "\x89PNG\r\n\x1a\n", a Java class begins with the four bytes (in hexadecimal) CA FE BA BE, and an HTML file has "< html" somewhere near the start. It's just some small sequence of data that's known to be in a file of that type, usually very close to the start.
When people are defining file formats they often include one of these in it either deliberately or just as part of making the format fit together.
file can uses them afterwards. It also has other ways of actually looking at the contents of the file to guess what it is ("language tests").