Practice makes perfect! For the basics, I learned from a Buddhist teacher in person, which is probably the best way, but there's lots of information out there, for instance the
Wikipedia page on mindfulness, which mentions the use of meditation for chronic pain. I generally "follow the breath", which means focussing on the sensations (not the sounds) of breathing. Place your attention on the in and out movements of the stomach for relaxation and stabilization, or on the subtle sensations around the nostrils for improved focus. At first I counted breaths (in and out as one), up to ten, then starting again. Each time you lose count (and you will!), you should resist the temptation to get annoyed or frustrated, and just start again. Nowadays I just focus on the sensations. 20 minutes once or twice a day is probably plenty for most people, but some teachers say you can do more if you're unwell and you feel it helps.
Be peaceful!