A lot of spam is identified before it is even accepted by the mail server. A mail server always knows the IP address of the server that’s trying to send email to you. The first line of defense (and the oldest) is to refuse to accept email from places that have a bad reputation. Your ISP or email service provider (ESP) can maintain their own lists and/or can subscribe to third-party lists of “bad” IPs and simply refuse to accept email from them.
Besides outright refusing to accept email, your ESP can go ahead and accept the email and then use a wide variety of tools to determine whether or not to classify it as spam. Many systems use sophisticated scoring systems to “grade” the email. Does it come from a known good or bad IP? Does it contain spammy language? Does it include a link to a known spammer website? Does the website get DNS from a known spammer? Does the email contain a known spammer email address? Have other recipients classified similar emails as spam? Is the sender’s email address in the recipient’s address book? And so on and so forth.