Heart failure is common, complex, and often associated with coexisting chronic medical conditions and a high mortality. We aimed to assess the epidemiology of people admitted to hospital with heart failure with reduced ejection fraction (HFrEF) and heart failure with preserved ejection fraction (HFpEF), including the period covering the COVID-19 pandemic, which was previously not well characterised. In this retrospective, cohort study, we used whole-population electronic health records with 57 million individuals in England to identify patients hospitalised with heart failure as the primary diagnosis in any consultant episode of an in-patient admission to a National Health Service (NHS) hospital. We excluded individuals with less than 1 year of medical history records in primary or secondary care; admissions to NHS hospitals for which less than 10% of heart failure cases were linkable to the National Heart Failure Audit (NHFA); individuals younger than 18 years at the time of the heart failure hospitalisation; and patients who died in hospital during the index heart failure admission. For patients with new onset heart failure, we assessed incidence rates of 30-day and 1-year all-cause and cause-specific (cardiovascular, non-cardiovascular, and heart failure-related) emergency rehospitalisation and mortality after discharge, and dispensed guideline-recommended medical therapy (GRMT). Follow-up occurred from the index admission to the earliest occurrence of the event of interest, death, or end of data coverage. We estimated adjusted hazard ratios (HRs) to compare HFrEF with HFpEF. We computed population-attributable fractions to quantify the percentage of outcomes attributable to coexisting chronic medical conditions. Among 233 320 patients identified who survived the index heart failure admission across 335 NHS hospitals between Jan 1, 2019, and Dec 31, 2022, 101 320 (43·4%) had HFrEF, 71 910 (30·8%) had HFpEF, and 60 090 (25·8%) had an unknown classification. In patients with new onset heart failure, there were reductions in all-cause 30-day (-5·2% [95% CI -7·7 to -2·6] in 2019-22) and 1-year rehospitalisation rates (-3·9% [-6·6 to -1·2]). Declining 30-day rehospitalisation rates affected patients with HFpEF (-4·8% [-9·2 to -0·2]) and HFrEF (-6·2% [-10·5 to -1·6]), although 1-year rates were not statistically significant for patients with HFpEF (-2·2% [-6·6 to 2·3] vs -5·7% [-10·6 to -0·5] for HFrEF). There were no temporal trends in incidence rates of 30-day or 1-year mortality after discharge. The rates of all-cause (HR 1·20 [1·18-1·22]) and cause-specific rehospitalisation were uniformly higher in those with HFpEF than those with HFrEF. Patients with HFpEF also had higher rates of 1-year all-cause mortality after discharge (HR 1·07 [1·05-1·09]), driven by excess risk of non-cardiovascular death (HR 1·25 [1·21-1·29]). Rates of rehospitalisation and mortality were highest in patients with coexisting chronic kidney disease, chronic obstructive pulmonary disease, dementia, and liver disease. Chronic kidney disease contributed to 6·5% (5·6-7·4) of rehospitalisations within 1 year for HFrEF and 5·0% (4·1-5·9) of rehospitalisations for HFpEF, double that of any other coexisting condition. There was swift implementation of newer GRMT, but markedly lower dispensing of these medications in patients with coexisting chronic kidney disease. Rates of rehospitalisation in patients with heart failure in England have decreased during 2019-22. Further population health improvements could be reached through enhanced implementation of GRMT, particularly in patients with coexisting chronic kidney disease, who, despite being at high risk, remain undertreated. Wellcome Trust, Health Data Research UK, British Heart Foundation Data Science Centre.