Duration of efficacy is typically determined by tracking the antibody titers of a cohort of subjects who have gotten the vaccine, and estimating based on the trajectory of those titers where they will eventually cross the threshold to the point where the vaccine no longer confers immune resistance.
These estimates do get revised and estimated as time goes on - you will occasionally see new recommendations for a second or third "booster" dose of a vaccine, which is meant to extend the duration of immunity beyond the duration of the original vaccine.
Two major types of studies track this over time. Phase IV clinical trials, which are clinical trials required post-licensure, and observational epidemiology trials, which tend to be performed when disease transmission starts to occur in supposedly vaccinated populations.
Mechanical snail has raised the issue of viral evolution, so I'll touch on it briefly. The duration of efficacy discussed above is based on how long a patient's body can mount an immune response to a particular challenge. That's a concern for all vaccines.
For some vaccines, there's a secondary process that's of concern - that of the virus evolving in such a way that the antigens targeted by the vaccine are no longer those on the virus itself. This is only a concern for some viruses, notably those that are particularly fast-evolving, like influenza or HIV, and less of an issue for say, measles and HPV.
But that's typically not what people are talking about when they say "duration of efficacy" because it's inherently unpredictable, and less a function of the vaccine and more a function of the virus.