That's why there's a control group that received a placebo, who would act the same way as those who received the vaccine. In both the Pfizer and Moderna cases, the recent results released are based on the number of people who have contracted Covid, then they check which group they were in. If 100 people (out of the thousands in the study) get Covid, and 95 of them got the placebo, and 5 of them got the vaccine, you say the vaccine is 95% effective. It's pretty simple really, and the numbers are large enough that it may be off several percent, but can't be a total fluke.
Source?