Heart failure is the final destination of most cardiovascular diseases, and its complex molecular mechanisms remain largely uncertain. This study aimed to systematically investigate the underlying molecular mechanisms and diagnostic and therapeutic targets of heart failure using bioinformatics. We obtained 8 healthy samples and 8 heart failure samples from GSE8331 and GSE76701. After removing the batch effect, we performed a differential analysis on it and obtained 185 differentially expressed ID. The results of enrichment analysis showed that the molecular mechanisms of heart failure were mostly related to immune, inflammation, and metabolism-related pathways. Immune cell infiltration analysis showed that the degree of infiltration of Tgd cells and Neurons was significantly enriched in heart failure samples, whereas pDCs and NKTs were in healthy tissue samples. We obtained Hub genes including EGR1, EGR2, FOS and FOSB by PPI network analysis. We established a 4-gene diagnostic model with Hub gene, and validated it in GSE21610 and GSE57338, and evaluated the discriminative ability of Hub gene by ROC curve. The 4-gene diagnostic model has an AUC value of 0.775 in GSE21610 and 0.877 in GSE57338. In conclusion, we explored the underlying molecular mechanisms of heart failure and the immune cell infiltration environment of failing myocardium by performing bioinformatic analysis of the GEO dataset. In addition, we identified EGR1, EGR2, FOS and FOSB as potential diagnostic biomarkers and therapeutic targets for heart failure. More importantly, a diagnostic model of heart failure based on these 4 genes was developed, which leads to a new understanding of the pathogenesis of heart failure and may be an interesting target for future in-depth research.