If this problem comes and goes you
_may_ have defective DRAM (memory). Don't panic yet, it's only one possibility. However, if shutdown fails again immediately after it restarts go into the System Profiler (under /Applications/Utilities) and check the state of your DRAM (left column in System Profiler window -> Hardware -> Memory). The memory status should show "OK."
You may also want to run the hard disk and extended hardware tests.
First, run the verify disk and verify permissions checks using Apple's Disk Utility (located in /Applications/Utilities). If you find any problems make certain that you have backups of any important data and then run the repair options within Disk Utility. Also make certain that the disk's S.M.A.R.T. status is shown as "Verified" (look at lower right side of window in Disk Utility).
If the disk checks okay then you should run Apple's Hardware diagnostics. To do that, shutdown the computer, hold down the "D" key and restart the MacBook (while still holding down on the "D" key). Continue to hold the "D" key down and after a while a window should appear with instructions on how to run the diagnostics (at this time you may, of course, release the "D" key). The extended hardware tests take a good while to run, so you may want to run them after you've finished using the computer for the day (just plug the computer into the power supply and let it run overnight).
You also want to make certain that an external peripheral isn't causing this problem. If the problem occurs again then try disconnecting all of your external peripherals and try the shutdown again (several times to verify working or not).
You could also have a defective Mac OS X system installation. You might be able to eliminate that possibility by booting from your Mac OS X installation DVD (you can use that DVD to boot and shutdown your computer as a test, you don't need to re-install the system software). After you boot into the installer screen (from the DVD) look under the Utilities menu and select the startup disk option, select your internal drive, then select from the menu Startup Disk -> Shut Down). Then when you restart the MacBook hold down on the mouse/trackpad button while the system boots (this should eject the DVD before the system restarts).