Good question. Essentially, i5 is the same CPU as i7, only that its hyper-threading option is factory-disabled. If the hyper-threading is disabled in the BIOS for an i7 CPU, then it would have the same performance as a comparable i5.
So how can Intel i5 CPU be faster than i7? In single-thread workloads, a comparable i5 can be faster if it has a higher basic clock speed. Note that I say “comparable” meaning of the same generation.
Consequently, Intel CPU is evolving and for example, newer i5 may be faster than a much older i7. How is that possible?
Well, just take a look at the newer i5–8400 which is a six-core CPU with turbo up to 3.8GHz, as it is a recent model so it is much newer than an old i7–2700k and consequently it performs much better than its older sibling.
When PassMark benchmarking software is used to compare them, i5–8400 will score 11640 multicore and 2336 single-thread score, while the i7–2700k multi-core score is 8687 and 2005 points single-thread.
Now, if you compare current-generation Intel CPU then a modern i7 which is not “gimped” in any way will always perform better than a comparable i5 model.