You pay the fees on application and if the deal completes, the Lender will refund the valuation fee up to a given limit.
On completion, the Lender will release a cash sum, intended to help pay your legal fees
On completion, the Lender will release a cash sum, usually a percentage of the loan, for any purpose you want.
In all cases, make sure these are genuine cash back offers, not a further advance in disguise !
See also Redemption Penalties