A bakery that refused to sell wedding cakes to same-sex couples has been ordered to do so following a court battle.