Your camera's shutter consists of two curtains. These are called the 1st curtain and the 2nd curtain (or as some photographers like to call them the front curtain and the rear curtain). These two curtains are normally shut to prevent light from reaching the digital sensor or photographic film.
When taking an exposure of a scene, you typically press the Curtains shutter release button. What happens then is that the first curtain opens, to expose the sensor or film to light and register your scene, and the second curtain then follows it. At the end of the exposure both curtains are then reset to their original position for the next shot.
2nd curtain sync (or rear sync) is a photography technique that is usually used when shooting in low light conditions, with a slow shutter speed and an on-camera flash.
When you're shooting with an on-camera flash, the flash either fires right after the first curtain opens or it fires just before the second curtain starts to close. If the flash fires right after the first curtain opens, that is called 1st curtain sync (or front sync). If the flash fires right before the second curtain starts to close to end the exposure, that is called 2nd curtain sync (or rear sync).
The default setting on cameras is the 1st curtain synchronization mode. You can of course change your flash sync mode from the camera's menu settings or the speedlight itself to have your flash fire in 2nd curtain synchronization mode.
So why would you want to set your camera to 2nd curtain sync mode?
2nd curtain sync is useful when shooting with an on-camera flash with long shutter speeds. During long exposures, any motion or moving objects will appear blurred and have tell-tale trails in the final image indicating the course of their movement.
Now, imagine your flash is set to fire once the first curtain opens and you have a moving subject. The curtain opens, the scene starts to record onto your sensor or film. The flash fires and consequently your main subject in the frame is frozen in time at whatever state they're at. Now since the exposure time is long, your subject will normally start to move but the sensor is still exposed, right?! Now everything in the scene is still getting recorded including your moving subject. At the end of the exposure, the second curtain starts to close and the exposure is ended.
Lets go back a little bit: your subject is frozen, they start to move, their movement is registered blurry. Because the subject is first frozen and then their movement is recorded afterwards, you will have the tell tail trails in front of them in the final image. Now this might be what you want, in which case the result you got is ok. But logically, you would want the blurry movement recorded first and then your subject frozen leaving its tell tail trails behind them instead of in front of them, right?! Most of the time yeah, unless you're going for a different effect.
So what do you do to get your subject's blurred trails of motion behind them? You simply set your flash to fire in 2nd curtain synchronization mode! When you set your camera to 2nd curtain sync, the first curtain opens and everything going on in the scene is being recorded. Now, since the exposure time is long, all these happenings are being blurred. As the exposure comes to an end, the second curtain will start to close. Since you're in 2nd curtain synchronization mode, the flash will fire right before the second curtain starts to shut. This way, you have all motion blur trails registered, and then at the end of your exposure the flash fires freezing your subject at whichever state they are at the moment, and you have your motion blur behind the subject instead of in front of them. Pretty neat, huh?!!
How to set your camera to 2nd curtain sync mode?
How to set your flash to fire in 2nd curtain sync mode depends on your type of camera. Canon cameras allow you to set 2nd curtain sync mode either from the camera settings menu or from the back of the speedlight itself. With Nikon, you can only set the on-camera flash to 2nd curtain sync from your camera's settings menu.
To set your flash to 2nd curtain synchronization from the camera menu, browse to the Flash Control menu (the naming might differ depending on your manufacturer, but it's gonna be something very similar). From there, scroll down to the flash function settings, and Curtains click OK to enter that menu. Scroll down to shutter curtain synchronization and choose 2nd curtain sync, instead of the already pre-set 1st curtain sync option.
To set your flash to 2nd curtain sync from the back of the speedlight itself (this works for Canon users and not for Nikon users), you have a button at the back of your flash gun with a drawing that looks something like a lightning bolt with the letter "H" accompanied by three adjacent triangles. Hitting this button time after time lets you cycle among the different available flash modes you can choose from. Hit the button a couple times and you'll see that drawing of the three adjacent triangles light up on your speedlight's LCD screen, this means you're then in 2nd curtain synchronization mode.