There are fewer attacks and malware on Mac OS X systems for a variety of reasons, almost none of which having any relation with the notion of "software quality":
There are fewer OS X systems than Windows systems (right now, about 7.5% of computers involved in Internet browsing use OS X, according to StatCounter), making OS X a less interesting target for malware authors. Developing malware costs efforts and time, so the malware authors want to get a good return over investment. So they hunt where there are the most preys, i.e. on Windows, not OS X.
Malware usually enters a machine through explicit user action, i.e. installation of some software package that turned out to be dodgy. OS X users tend to install less third-party packages, mostly because OS X comes out-of-the-box with more accessories.
In a similar vein, when it comes to supporting hardware, OS X has its own drivers, so the normal Mac user plugs his new device: either it works right away, or it will never work. There is usually little point, in the OS X world, to try to download and install third-party drivers. On the other hand, for Windows, driver download is the norm. This does not mean that third-party drivers are often infected, but it implies that Windows users are accustomed to downloading executables and running them with full administrator rights.
Still on the same subject, Windows has long suffered from something known as DLL Hell, coming from the traditional method of identifying DLL only by a short file name, with no versioning support. This has trained many Windows users to "solve issues" by looking for the faulty DLL name on Google and then downloading it from the first link that arises. OS X, on the other hand, is on the habit of embedding needed libraries in the application, which avoids the issue (at the expense of disk space, since it makes applications larger -- that's rarely a problem with today's disks).
The main characteristic of Windows is its backward compatibility: that which was once running in any version of Windows, will keep running. This has made the system API very complex through decades of accumulation; and this complexity helps malware authors in the hide-and-seek game that they play with antivirus authors. Backward compatibility is much less maintained in the Mac OS world (Apple even switched complete architectures several times, switching from Motorola 680x0 CPU to PowerPC, and then to Intel x86; and support of 32-bit x86 application code can be predicted to be removed at some point).
It must still be said that what makes a machine secure is not really its software, but its system administration, i.e. what the user makes with the machine. Most of the points above are about how OS X users do not behave the same way as Windows users, for a lot of reasons, some of them being historical.