Sharif led a high-level delegation to Kabul, including Pakistani army chief Gen Raheel Sharif and the head of the country's powerful ISI intelligence agency, Gen Rizwan Akhtar.
"Afghanistan and Pakistan should fight together to bring the security and stability and prosperity which our people want," Ghani said during a joint press conference. "The enemy of Pakistan is the enemy of Afghanistan and the enemy of Afghanistan is the enemy of Pakistan."
Both nations are combating branches of the militant Taliban group.
Sharif pledged that any militant group seeking to destabilise Afghanistan from Pakistani soil will be hunted down and dealt with harshly.
"Coordinated operations will be planned and conducted on mutually agreed basis to target militant hideouts along the border," he said.
Sharif also condemned the current offensive by the Afghan Taliban in the northern province of Kunduz, and endorsed Ghani's reconciliation initiative that aims to bring his country's Taliban to the negotiating table.