This is one of those hard to explain without diagrams and several interest rate calculators doing comparison.
So, I'll see if I can help a bit without the numbers getting in the way. When you take out a loan, the interest paid in the beginning is very high, and the principle reduction is very small. That principle amount in the early part of the loan lets the bank make it's money back up front. Making curtailment payments(principal reduction) early in the loan helps a great deal to reduce the remaining principal, but after the half way point of the loan, there's not as much incentive to pay the loan principal down.
The next issues is the time value of money. It's why people borrow money to start with. If they had enough to pay in cash there would be no reason for a loan. Frex; paying $5000 today in refi costs is worth more than $5000 paid a few bucks at a time over 30 years. The money is worth more today, than it will be in 20-30 years.
Now, the delta between when it's advantageous to refi depends on the original rate, the number of payments made already, any additional principal paid against the load(thus shortening the payoff date), the interest rate of the new loan, and of course the up front refi costs.
Generally speaking, and this is a very generalized statement, with a conforming loan today, it doesn't make sense to refi until you can get an approx 2% reduction in interest rate, while limiting the refi costs to less than 2% of the loan amount. See how that works? Just try to remember the 2% delta. If the current rate is 5%, and it's still in the first half of the loan payments, and you haven't made too many early principal payments, then refi at the same term(30 years) would need to be ~3% interest rate, and refi would need to be around 2% of the loan amount.
Now, when I say generally, I mean there are a ton of variables. If the refi costs up front are only a few hundred bucks, then you don't need to get the 2% reduction in interest rate. If the refi costs are very high, like $6000, then the interest rate needs to be well under 2% lower to justify the up front time value of money, where that amount could be applied to the current loan principal reduction and would shorten the payoff term.
Best to take all variables into account, use a couple different calculators and figure it out. I would like to see someone change from a 30 year to a 15 year where the interest paid is lowered significantly. In this case you don't need the 2% delta to affect a better situation.