Palestinian factions including rivals Hamas and Fatah have signed an agreement to end their divisions and form a unity government followings talks held in Beijing.