If they're still on the phone you can use an SMS-backup app. Note that some devices only allow backing up SMS, so you might be losing MMS. If you're transferring to another phone the manufacturer might have a backup option, like Samsung's SmartSwitch.