Originates from Mongolian horse culture. When meeting someone, you would pat their horse's rump and praise it — a way to flatter the owner. Over time it came to mean empty flattery.
grammar
Can be split: 拍某人的马屁 (to flatter someone). The object goes between 拍 and 马屁.