The borg queen is defeated when Picard assimilates himself, and talks Jack into waking up out of the trance he's in and decide to join him in leaving the borg. The crew beams Picard and his son Jack up, along with Word and Riker, and the borg queen is left alone and destroyed in the fire caused by the explosion from when the beacon was taken out inside the borg cube.
Earth is saved because without the beacon and Jack commanding all of the borg converts to attack earth, and the borg queen killed everyone who was assimilated has returned to normal. Dr. Crusher figures out how to remove the borg code, originally from Picard, that was planted by the transporters into every young person who used them. The last scene appears to be the old crew playing poker: Picard, Dr. Crusher, Worf, Riker, Troi, Data -- but Guinan is notably missing.