While you can implement this using SDL_WarpCursor(), I've run into problems with that method on some platforms. I've had real problems with some platforms not reliably performing the WarpCursor() action, particularly when I've been calling it every frame.
Also, remember that on many platforms, the cursor is handled at a higher frequency than your app. This is particularly true if you're running below 60fps! So even if the platform is successfully warping the mouse on every frame of your game, the cursor could still get away from your window if it's receiving updates in between your rendered frames.
The correct method for trapping the mouse under SDL is:
SDL_WM_GrabInput( SDL_GRAB_ON );
This tells SDL that your intention is to actually grab full control of mouse + keyboard (see the documentation), and to therefore keep the mouse inside the window no matter what. From this point on, the mouse will not move outside of the window, regardless of your frame rate, and you should not call SDL_WarpCursor, except if you really do intend to teleport the cursor somewhere, rather than spamming it continuously.
While in this "grabbing" mode, you will continue to receive mouse motion events as though the mouse was not being constrained inside the window. (So if the cursor is at the right edge of the screen, and the user moves his mouse further right, you'll receive a mouse motion event showing motion to the right, though the cursor position will not change). This is really useful for (for example) turning controls for FPS games, where you only care about relative motion of the cursor, not its absolute position.
Update for SDL2 -- 9 December, 2013
This interface has changed for SDL2. If you're using SDL1.2, the answer above is still correct. However, under SDL2 the SDL_WM_GrabInput(SDL_GrabMode)
function is no longer available. SDL2's new way to capture the mouse is:
SDL_SetRelativeMouseMode(SDL_TRUE);
Otherwise, this is identical to the earlier function -- in this mode you will continue to receive SDL_MOUSEMOTION
events as though the mouse wasn't being constrained inside the window.